2012-01-19 73 views
1
//user.js 

couchdb = require('couchdb'); 

exports.create = function(req, res){ 

    user = req.body 

    if(validate(user)) { 

    couchdb('db').insert(user); 
    //redirect 

    } else { 

    //render new again with the user 

    }; 

}; 

我想测试上述函数是否创建了一个用户。模拟外部依赖

//user_spec.js 

describe('User create', function(){ 

    beforeEach(function(){ 
    //call create with valid user 
    }); 

    it('should create a user', function(){ 

    //test database for user 
    assert(fakeDatabase.users.length, 1) 

    }); 

}) 

有没有人知道一个方法来替换一个假的couchdb对象,所以我可以测试一个用户是否被创建。我真的不想在我的单元测试中调用真正的couchdb。

回答

1

尝试nock。您可以模拟http(s)请求,但是您必须跟踪您自己添加的用户。

var scope = nock('http://myapp.iriscouch.com') 
       .get('/') 
       .reply(200, {username: 'pgte', email: '[email protected]', _id: "4324243fsd"}); 
+0

认为问题的标题引起了一些混淆。我想嘲笑模块内的依赖关系。诺克虽然看起来不错。 – Pickels