尝试首次使用Sinon,因为它的假服务器功能让我存根API响应。测试本身是为摩卡书写的Sinon假服务器不拦截请求
但是,假的服务器似乎不会拦截请求。
代码:
describe('when integrated', function() {
var server;
beforeEach(function() {
server = sinon.createFakeServer();
});
afterEach(function() {
server.restore();
});
it('can send a message to the notification service', function() {
server.respondWith("POST", new RegExp('.*/api/notificationmanager/messages.*'),
[200,
{ "Content-Type": "application/json" },
'{ "messageId":23561}'
]);
var messageOnly = new PushMessage(initMessageObj);
var originalUrl = PushMessage.serverUrl;
messageOnly.setServerAPI("http://a.fake.server/api/notificationmanager/messages");
console.log("fake server is: ", server);
messageOnly.notify()
.then(function(response) {
messageOnly.setServerAPI(originalUrl);
return response;
})
.then(function(response) {
response.should.be.above(0);
})
console.log(server.requests);
server.respond();
})
});
作为参考,PushMessage
是具有静态属性serverUrl
的对象。我只是将该值设置为假的URL &,然后重置它。 notify()
函数使用request-promise-native
发送邮件消息到设置在PushMessage的静态属性中的serverUrl
。
什么似乎发生,就是POST
请求最终被正确地试图对的http://a.fake.server/api/notificationmanager/messages
的URL,从而导致错误的地址不存在...
任何想法,我做错了...?谢谢!
尔加。从来没有想过一起谷歌假服务器和节点...谢谢! –