我有一个通过调用2个函数作为参数来调用服务函数的控制器(取决于服务函数的结果来调用),如下所示:
控制器代码:
onSuccess(data){ ... }
onError(data){ ... }
service.post(url, query, onSuccess, onError);
服务代码:
var service = function($log, $http, $location, $interval) {
...
var post = function(url, query, onSuccess, onError){
$http.post(url, query)
.success(function(data, status, headers, config){
onSuccess(data);
})
.error(function(data, status, headers, config){
onError(data);
});
};
...
});
我想做的是测试传递到post函数中的查询是否正确,因此调用位于控制器代码内的onSuccess函数。我试过这样测试:
测试代码:
describe("post", inject(function (service) {
beforeEach(module('myApp'));
beforeEach(function() {
var url = "http://..."
var query = {
...
}
var mockSuccess = jasmine.createSpy('Success function');
var mockError = jasmine.createSpy('Error function');
service.post(url, query, mockSuccess, mockError);
}
it('Should call success function following call to http.post', function () {
expect(mockSuccess).toHaveBeenCalled();
});
))};
尽管出现错误:
TypeError: null is not an object (evaluating 'currentSpec.$modules')
所以我不确定如何正确测试我的查询是否通过http请求成功返回了数据?
谢谢。