[英]Unittest case using Jasmine for http post method
I have tried to write a unit test case for post method in angular service. 我试图为角度服务中的post方法编写一个单元测试用例。 I got $http is undefined error.
我得到了$ http是未定义的错误。 below is my code.
下面是我的代码。 any one tell me what i am missing.
有人告诉我我想念的东西。 i am adding module using separate file.
我正在使用单独的文件添加模块。 service code
服务代码
sample.factory('AddProductTypeService', function () {
return {
exciteText: function (msg) {
return msg + '!!!'
},
saveProductType: function (productType) {
var result = $http({
url: "/Home/AddProductTypes",
method: "POST",
data: { productType: productType }
}).then(function (res) {
return res;
});
return result;
}
};
});
Jasmine 茉莉花
describe("AddProductTypeService UnitTests", function () {
var $rootScope, $scope, $factory, $httpBackend, basicService,createController, authRequestHandler;
beforeEach(function () {
module('sampleApp');
inject(function ($injector) {
basicService = $injector.get('AddProductTypeService');
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
});
});
// check to see if it does what it's supposed to do.
it('should make text exciting', function () {
var result = basicService.exciteText('bar');
expect(result).toEqual('bar!!!');
});
it('should invoke service with right paramaeters', function () {
$httpBackend.expectPOST('Home/AddProductTypes', {
"productType": "testUser"
}).respond({});
basicService.saveProductType('productType');
$httpBackend.flush();
});
});
error : 错误:
ReferenceError: $http is not defined ReferenceError:未定义$ http
Thanks in advance 提前致谢
You have to inject the $http
service into your service 您必须将
$http
服务注入到您的服务中
sample.factory('AddProductTypeService', ['$http' ,function ($http) {
/* ... */
}]);
https://docs.angularjs.org/guide/di https://docs.angularjs.org/guide/di
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.