让服务
function testService(){
function test(){
return{
find:find,
save:save
};
function find(){
//return a promise
}
function save(){
//some code
}
}
return {test:test};
}
让控制器
function TestController(testService){
var ctrl=this;
ctrl.first=first;
function first(){
testService.test().find().then(function(response){});
}
}
我怎样才能在第一个'中模拟服务电话?使用jasmine控制器中的函数
答案 0 :(得分:1)
像这样......
设置
const testSpy = jasmine.createSpyObj('testService.test', ['find', 'save'])
const testServiceSpy = jasmine.createSpyObj('testService', ['test'])
const reponse = {
status: 200
}
testServiceSpy.test.and.returnValue(testSpy)
testSpy.find.and.returnValue(Promise.resolve(response))
并在你的测试中
const testController = new TestController(testServiceSpy)
testController.first()
expect(testServiceSpy.test).toHaveBeenCalled()
expect(test.find).toHaveBeenCalled()
答案 1 :(得分:0)
spyOn(testService, 'test').and.callFake(function () {
return { find: function () {
return {
then: function (callback) {
return callback({ 'status': 3 }); }
};
}
};
});
ctrl.first(); 期望(testService.test).toHaveBeenCalled();
我用这种方法找到了工作