Angularjs http的Jasmine单元测试
我试图围绕一个角度服务编写单元测试用例。我基本上要测试的是http请求是否发出 服务Angularjs http的Jasmine单元测试,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我试图围绕一个角度服务编写单元测试用例。我基本上要测试的是http请求是否发出 服务 app.service("myService", [ "$http", function ($http) { this.result = "initial value"; this.get = function (param1, param2) { return $http({ method: "GET",
app.service("myService", [
"$http", function ($http) {
this.result = "initial value";
this.get = function (param1, param2) {
return $http({
method: "GET",
url: "api/someService/Get/" + param1 + "/" + param2
})
.success(function (data) {
this.parent.result = data;
console.log(data);
console.log(this.parent.result);
return data;
})
.error(function () {
return "Error occurred";
})
;
};
}
]);
下面是我试过的
describe("Surcharge Increase Formula", function () {
var controller;
var scope;
var httpBackend;
var myService;
beforeEach(inject(function($controller, $rootScope, $httpBackend, _myService_) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
myService = _myService_;
});
it('waiver service makes API call', function () {
var response = "This is the response";
httpBackend.when('GET', "api/SurchargeWaiver/Get/0/0").respond(response);
waiverService.get(0, 0);
//httpBackend.flush();
expect(waiverService.result).toEqual(response);
});
我想做的是做一个与expect…类似的调用,以查看是否进行了api调用,或者以任何其他方式测试http调用
有什么建议吗?使用像sinon这样的模拟库,可以1/在beforeach中获得对$http服务的引用;和2/使用“sinon.spy”对其进行监视;工作不太熟悉角度注入,对不起,我有一个使用httpBackend模拟到$http的方法,但是我会监视什么方法来查看它是否被调用?看起来$http本身就是一个函数,你能监视它吗?