我正在使用Karma和Jasmine进行单元测试。我有app.js作为主要源文件:
app.service("someServ", function(){
this.sendMsg = function(name){
return "Hello " + name;
}
})
app.factory("appFactory", function ($q, someServ) {
function getData() {
var defer = $q.defer();
defer.resolve("Success message");
return defer.promise;
}
function foo(){
var text = someServ.sendMsg("Message");
alert(text);
}
return {
getData : getData,
foo : foo
}
})
app.controller("mainController",['$scope','$http','appFactory',function($scope, $http, appFactory){
var mct = this;
mct.printData = function(){
var myPromise = appFactory.getData();
myPromise
.then(function(data){
alert("Promise returned successfully. Data : " + data);
}, function(error){
alert("Something went wrong.... Error: " + error);
})
}
mct.showMsg = function(){
appFactory.foo();
}
}]);
我的testFile.js如下:
beforeEach(module(function($provide){
$provide.service("someServ", function(){
this.sendMsg = jasmine.createSpy('sendMsg').and.callFake(function(param){})
});
$provide.factory("appFactory", function(someServ, $q){
function getData(){
var defer = $q.defer();
defer.resolve("Success message");
return defer.promise;
}
function foo(){
var facParam = "some text";
someServ.sendMsg(facParam);
}
return {
getData : getData,
foo : foo
}
});
}));
var $scope, mainController, appFactoryMock, someServMock;
beforeEach(inject(function($rootScope, $controller, $http, $q, appFactory, someServ){
appFactoryMock = appFactory;
someServMock = someServ;
$scope = $rootScope.$new();
mainController = $controller("mainController", {
$scope : $scope,
$http : $http,
appFactory : appFactoryMock
});
}));
it('that mainController is calling appFactory methods', function(){
spyOn(appFactoryMock, "getData");
mainController.printData();
scope.$root.$digest();
expect(appFactoryMock.getData).toHaveBeenCalled();
})
it('that appFactory method foo calls someServ sendMsg', function(){
spyOn(appFactoryMock, "foo");
appFactoryMock.foo();
expect(someServMock.sendMsg).toHaveBeenCalled();
});
以上两项测试都失败了。对于第一个,错误是:Cannot read property of undefined
,第二个错误:expected spy sendMsg to have been called
。第一个错误发生在:app.js文件中,如调用堆栈中所示。我还在karma chrome窗口中使用Debug选项调试了我的测试。 printData()
函数在app.js中调用实际代码,但我已经嘲笑过了。
请有人解释我为什么会这样,以及如何解决这个问题?为什么要调用原始代码,如何让这两个测试通过。
答案 0 :(得分:3)
Jasmine的间谍只会检查函数是否已被调用,而不会触发实际的实现。这就是getData().then
抛出错误的原因。
正如您可以阅读on Jasmine's documentation,您需要添加.and.callThrough()
才能完成原始功能。
答案 1 :(得分:1)
我认为第一个问题可能与代码中的语法错误有关 - 在您的第一个it
块中,scope
变量引用了什么?