最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

模拟内部依赖项

IT培训 admin 6浏览 0评论

模拟内部依赖项

我有一个对Facebook图形对象具有内部依赖性的请求,该对象对FB图形API执行另一个请求。

[我想知道是否可以使用sinon来模拟图形对象,以便它实际上不会在测试中执行请求,而是使用我在测试中提供的值执行回调函数。

server.post("/facebookLogin", function(req, res) {
    graph.setAccessToken(req.body.fbtoken);

    graph.get("me?fields=email", function(err, obj) {
        if (!err) {
            var email = obj.email;

            checkUserAlreadyRegistered(email, function(user) {
                if (user) {
                    return res.send(200, {user:user, token: decorateToken(user.id)});
                } else {
                    return res.send(404);
                }            
            });
        } else {
            return res.send(500);
        }        
    });
});
回答如下:

我遇到了完全相同的问题,并深入研究fbgraph源代码I found out,即使它使用的是“ graphql”,在内部也是request的网络请求,因此您可以轻松地使用nock进行拦截:] >


// https://github/criso/fbgraph/blob/master/lib/graph.js#L34 <-- fb graph url

const fbMock = nock('https://graph.facebook/v4.0/')
 .get('/me')
 .query(true)
 .reply(200, {
   id: '123123',
   name: 'fb username',
   email: '[email protected]'
 })

it('should not call fb"', (done) => {

  chai.request(server)
   .post('/facebookLogin')
   .send({ fbtoken: 'token_fb' })
   .end((err, res) => {
     expect(err).to.be.null
     expect(res).to.have.status(200)
     expect(fbMock).to.have.been.requested
     done()
   })
}

note:

/v4.0/部分可能会有所不同,具体取决于您的配置,但默认值为2.9,因此请确保使用与setVersion方法设置的相同的部分

模拟内部依赖项

我有一个对Facebook图形对象具有内部依赖性的请求,该对象对FB图形API执行另一个请求。

[我想知道是否可以使用sinon来模拟图形对象,以便它实际上不会在测试中执行请求,而是使用我在测试中提供的值执行回调函数。

server.post("/facebookLogin", function(req, res) {
    graph.setAccessToken(req.body.fbtoken);

    graph.get("me?fields=email", function(err, obj) {
        if (!err) {
            var email = obj.email;

            checkUserAlreadyRegistered(email, function(user) {
                if (user) {
                    return res.send(200, {user:user, token: decorateToken(user.id)});
                } else {
                    return res.send(404);
                }            
            });
        } else {
            return res.send(500);
        }        
    });
});
回答如下:

我遇到了完全相同的问题,并深入研究fbgraph源代码I found out,即使它使用的是“ graphql”,在内部也是request的网络请求,因此您可以轻松地使用nock进行拦截:] >


// https://github/criso/fbgraph/blob/master/lib/graph.js#L34 <-- fb graph url

const fbMock = nock('https://graph.facebook/v4.0/')
 .get('/me')
 .query(true)
 .reply(200, {
   id: '123123',
   name: 'fb username',
   email: '[email protected]'
 })

it('should not call fb"', (done) => {

  chai.request(server)
   .post('/facebookLogin')
   .send({ fbtoken: 'token_fb' })
   .end((err, res) => {
     expect(err).to.be.null
     expect(res).to.have.status(200)
     expect(fbMock).to.have.been.requested
     done()
   })
}

note:

/v4.0/部分可能会有所不同,具体取决于您的配置,但默认值为2.9,因此请确保使用与setVersion方法设置的相同的部分

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论