我正在尝试学习NodeJS的单元测试。我正在尝试Jasmine框架。
我写了一个规范来测试请求是否调用了快速控制器功能。
这是我的规格文件
var request = require("request");
var base_url = "http://localhost:3000/";
var mock = require('mock-require');
describe("Testing hotelSearchController", function(){
var server, mockHandle;
beforeEach(function(){
mockHandle = {
handleSearchRequest : (req, res) => { res.send("hi");}
}
var hotelSearchController = {
handleSearchRequest: mockHandle.handleSearchRequest
};
mock("../../app/controllers/hotelSearchController", hotelSearchController);
server = require("../utils/server")();
});
afterEach((done)=>{
mock.stopAll();
server.close(done);
});
it('should call handleSearchRequest()', function(done) {
var spy = spyOn(mockHandle, "handleSearchRequest").and.callThrough();
request.get(base_url+"search", function(error, response, body) {
expect(body).toBe("a");
expect(spy).toHaveBeenCalled();
done();
});
});
});
我的问题是,当我运行测试时,不遵守控制器模块的模拟,而是调用了实际的模块及其功能。同样,间谍也失败了。有人可以帮我吗?