2012-11-10 31 views
8

Tôi tiếp tục gặp lỗi trong phương thức save() khi chạy thử nghiệm.Tôi nên dùng mocha và mongoose như thế nào?

var User = require('../../models/user') 
, should = require('should'); 

describe('User', function(){ 
    describe('#save()', function(){ 
    it('should save without error', function(done){ 
     var user = new User({ 
     username : 'User1' 
     , email  : '[email protected]' 
     , password : 'foo' 
     }); 
     user.save(function(err, user){ 
     if (err) throw err; 

     it('should have a username', function(done){ 
      user.should.have.property('username', 'User1'); 
      done(); 
     }); 
     }); 
    }) 
    }) 

}) 

đây là lỗi:

$ mocha test/unit/user.js 

    ․ 

    ✖ 1 of 1 test failed: 

    1) User #save() should save without error: 
    Error: timeout of 2000ms exceeded 
     at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:1 
61:14) 
     at Timer.list.ontimeout (timers.js:101:19) 
+0

Làm sao chúng ta có thể thiết lập 'sở hữu username' từ chức năng thực để chúng ta có được sở hữu Tên truy nhập trong 'user.should.have. thuộc tính ('username', 'User1'); '? Chúng ta có thể gửi như 'res.send ({username: 'User1'})' hay 'res.render ('home', {username: 'User1'})' trong hàm thực? –

Trả lời

7

Bạn có thể làm tổ mô tả nhưng không phải nó kiểm tra. Mỗi bài kiểm tra có nghĩa là để được đứng một mình vì vậy khi bạn đang tìm kiếm thông qua kết quả của bạn, bạn có thể nhìn thấy nơi nó không - trên lưu hoặc không có tài sản tên người dùng. Ví dụ trong mã của bạn ở trên không có cách nào để nó thất bại 'nên lưu mà không có lỗi' kiểm tra như không có thực hiện(). Đó cũng là lý do mã ở trên là thời gian ra: mocha không thể tìm thấy được thực hiện() cho 'nên lưu mà không có lỗi' kiểm tra.

Có thể mô tả tổ rất mạnh mẽ. Trong mỗi mô tả, bạn có thể có một câu lệnh trước, trước khi, sau và sau đó. Với những cái này bạn có thể đạt được cái lồng mà bạn đang cố gắng ở trên. Xem tài liệu mocha để biết thêm thông tin nếu bạn muốn đọc trên các báo cáo này.

Con đường tôi sẽ viết những gì bạn đang cố gắng để đạt được như sau:

var User = require('../../models/user') 
    , should = require('should') 
    // this allows you to access fakeUser from each test 
    , fakeUser; 

describe('User', function(){ 
    beforeEach(function(done){ 
    fakeUser = { 
     username : 'User1' 
     , email  : '[email protected]' 
     , password : 'foo' 
    } 
    // this cleans out your database before each test 
    User.remove(done); 
    }); 

    describe('#save()', function(){ 
    var user; 
    // you can use beforeEach in each nested describe 
    beforeEach(function(done){ 
     user = new User(fakeUser); 
     done(); 
    } 

    // you are testing for errors when checking for properties 
    // no need for explicit save test 
    it('should have username property', function(done){ 
     user.save(function(err, user){ 
     // dont do this: if (err) throw err; - use a test 
     should.not.exist(err); 
     user.should.have.property('username', 'User1'); 
     done(); 
     }); 
    }); 

    // now try a negative test 
    it('should not save if username is not present', function(done){ 
     user.username = ''; 
     user.save(function(err, user){ 
     should.exist(err); 
     should.not.exist(user.username); 
     done(); 
     }); 
    }); 
    }); 
}); 
+0

Khi tôi chạy thử nghiệm này tôi nhận được: '\t \t nó ('nên có tài sản username', function (thực hiện) { \t \t ^^ Lỗi Cú pháp: identifier' bất ngờ nó không giống như "nó (" ' – chovy

+0

nevermind Tôi thấy lỗi này rất hữu ích – chovy

+0

Nếu tôi thêm một mô tả khác làm con thứ 2 của mô tả đầu tiên, User.rmeove() không có bất kỳ ảnh hưởng nào. – chovy

Các vấn đề liên quan