2014-10-10 15 views
5

Tôi cố gắng để chạy thử nghiệm buồm đơn vị của tôi (sử dụng mocha và istanbul)Sails js kiểm tra chạy

khi chạy

grunt test 

tôi nhận được lỗi

1) "before all" hook 
    2) "after all" hook 

    0 passing (5s) 
    2 failing 

    1) "before all" hook: 
    Error: timeout of 2000ms exceeded 
     at null.<anonymous> (/vagrant/node_modules/mocha/lib/runnable.js:157:19) 
     at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) 

    2) "after all" hook: 
    ReferenceError: sails is not defined 

các thiết lập không dường như tìm thấy Sails của tôi ... nhưng làm

which sails 

tôi nhận được

/usr/local/node/node-default/bin/sails 

và chạy sails lift hoạt động tốt

Dưới đây là file kiểm tra mocha trong dự án của tôi

//boostrap.test.js 
var Sails = require('sails'); 

before(function(done) { 
    Sails.lift({ 
    // configuration for testing purposes 
    }, function(err, sails) { 
    if (err) return done(err); 
    // here you can load fixtures, etc. 
    done(err, sails); 
    }); 
}); 

after(function(done) { 
    // here you can clear fixtures, etc. 
    sails.lower(done); 
}); 

Trả lời

4

Trước tiên, bạn có một vấn đề typo, bạn có:

var Sails = require('sails'); 

Nhưng bạn cố gắng sử dụng

sails.lower(done); 

Thay vì

Sails.lower(done); 

Sau đó, bạn cần phải xác định một cài đặt "di chuyển" toàn bộ dự án (http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html?q=migrate) Bạn chỉ cần chỉnh sửa confi Tệp g/models.js. Bạn có thể chỉ cần bỏ ghi chú dòng sau:

//migrate: 'alter' 

Enjoy :)

2

Tôi đã không nhìn thấy mã thực tế của trường hợp thử nghiệm của bạn, vì vậy Tôi chỉ liệt kê một vài khả năng ở đây.

Nếu bạn chạy thử nghiệm không đồng bộ, tôi nghĩ bạn cần đặt thuộc tính thời gian chờ trong mocha thành một số lớn hơn. Tôi đã viết ba trường hợp thử nghiệm rất đơn giản, nhưng tôi luôn luôn có được cùng một vấn đề như bạn mô tả. Tôi đã thử 10000ms, 20000ms và 30000ms. Khi tôi tăng nó lên 90000ms, tất cả các trường hợp thử nghiệm đều được thông qua. Vì vậy, tôi nghĩ rằng đó là vì buồm thực sự cần một thời gian để nâng trước khi thử nghiệm bắt đầu.

Một suy nghĩ khác là bạn đã đặt thuộc tính 'di chuyển' trong tệp cấu hình môi trường của mình? Nếu không, lệnh nâng cánh buồm sẽ đợi cho đầu vào của bạn chọn "thả", "thay đổi" hoặc "an toàn" trước khi tiếp tục, điều này cũng sẽ gây ra sự cố khi hết thời gian thử nghiệm.

1

Nó không giống như tôi biết vấn đề của bạn nhưng nó có thể giúp bạn tìm ra của bạn nếu tôi viết xuống một số snipets của các bài kiểm tra làm việc của tôi trong buồm. Tôi cũng bao gồm tệp package.json để bạn biết phiên bản của từng mô-đun.

đây là các module liên quan trong package.json:

rootproject/test/mocha.opts, điều này có lẽ những gì u cần

--timeout 5000 

rootproject/package.json

{ 
    ... 
    "dependencies": { 
    ... 
    "sails": "0.9.7", 
    "sails-disk": "~0.9.0", 
    "sails-memory": "^0.9.1", 
    "sails-mongo": "^0.9.7", 
    ... 
    }, 
    "devDependencies": { 
    "mocha": "^1.20.1", 
    "barrels": "^0.0.3", 
    "supervisor": "^0.6.0" 
    }, 
    "scripts": { 
    "start": "node app.js", 
    "debug": "node debug app.js", 
    "test": "PORT=9999 NODE_ENV=test mocha -R spec -b --recursive" 
    }, 
    "main": "app.js", 
    ... 
} 

Tôi cũng đã thêm một bộ điều hợp mô hình khác để sử dụng cho các thử nghiệm tại rootproject/config/adapters.js

test: { 
    module : 'sails-memory' 
}, 

rootproject/test/index.js

var assert = require('assert'); 
var Sails = require('sails'); 
var barrels = require('barrels'); 

var fixtures; 

var userTest = require('./controllers/User.js'); 
//... other test controllers ... 

//in case you need different simulations per controller you could add a custom Response in your test controller and use them instead 
var defaultCustomRequest = function(urlParams, bodyParams/*whatever else u need*/) { 
    //simulates the sails request 
    //create an object here, based on how u use the req object in your sails controllers 
    //.eg 
    return { 
    params: urlParams, 
    body: bodyParams 
    }; 
} 

//in case you need different simulations per controller or per method you could add multiple custom Responses in your test controller and use them instead 
var defaultCustomResponse = function(expectedCode, dataExpecting/*whatever else u need*/) { 
    //simulates the sails res which I was using like this: res.status(httpCode).json({somedata}) 
    //use the assert function here to validate 
    //.eg 
    status: function (responseCode){ 
    assert(expectedCode === responseCode, 'Expected status is ' + expectedCode + ' but ' + responseCode + ' was returned.'); 
    return this; 
    }, 
    json: function (responseData){ 
    //assert your responseData with your expectedData 
    return this; 
    } 
    return this; 
}, 
} 

before(function (done) { 
    // Lift Sails with test database 
    Sails.lift({ 
    log: { 
     level: 'error' 
    }, 
    adapters: { 
     default: 'test' 
    } 
    }, function(err, sails) { 
    if (err) 
     return done(err); 
    // Load fixtures 
    barrels.populate(function(err) { 
     done(err, sails); 
    }); 
    // Save original objects in `fixtures` variable 
    fixtures = barrels.objects; 
    }); 
}); 

// Global after hook 
after(function (done) { 
    //console.log('fixtures loaded: ' + JSON.stringify(fixtures)); 
    sails.lower(done); 
}); 

describe('User', function() { userTest.run(fixtures, customRequest, customRespose); }); 
//or describe('User', function() { userTest.run(fixtures); }); 
//... other test controllers ... 

rootproject/test/controllers/User.js

var assert = require('assert'); 

var UserController = require('../../api/controllers/UserController.js'); 

module.exports = { 

    run: function(fixtures, customReq, customRes) { 
    describe('create', function(customReq, customRes) { 
     it ('should create a few user entries', function() { 
     if (!customReq) customReq = {/*custom request for user create*/}; 
     if (!customRes) customRes = {/*custom response for user create*/}; 

     //call your controllers for testing here 
     //.eg 
     UserController.create(
     new req({}, 
       {email: '[email protected]', password: 'password'}) 
     ,new res(201, 
       {email: '[email protected]', password: null}); 
     UserController.create(
     new req({}, 
       {email: '[email protected]', password: 'password'}) 
     ,new res(400, 
       {error: true}); 
    ); 

     }); 
    }); 

    //... more guns 
    } 
}; 

Như bạn có thể thấy trên gói .json Tôi đã sử dụng buồm 0.9.7, do đó có thể cần thêm thay đổi cho phiên bản 0.10.x. .eg thay vì config/adapters.js có các cấu hình config/models.js config/connections.js được sử dụng.

0

Câu trả lời được chấp nhận là chính xác cho một trong các lỗi.

Đối với các lỗi timeout chỉ cần thêm

this.timeout(5000); 

trên dòng sau

before(function(done) { 
Các vấn đề liên quan