angular单元测试http请求问题
angular unit test http request issue
我正在尝试为我的 http 请求编写单元测试
在我的控制器中,我有类似的东西
$scope.test1 = function(url) {
productFactory.getProduct(url)
.then(function(products){
$scope.result = products;
})
}
在我单位厂档
angular.module('myApp').factory('productFactory', function($http) {
var factoryObj = {};
factoryObj.getProduct = function(url) {
return http.get('/product/' + url)
}
return factoryObj
})
在我的单元测试中,我不确定如何编写测试,因为 url 是动态的
describe('test here', function () {
var testCtrl, scope, httpBackend, mockFactory;
// Initialize the controller and a mock scope
beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_, _productFactory_) {
scope = _$rootScope_.$new();
httpBackend = _$httpBackend_;
mockFactory = _productFactory_;
testCtrl = _$controller_('testCtrl', {
$scope: scope
});
// the url is dynamic so I am not sure what to write here.
var url = 12345 <----dynamic product id
httpBackend.whenGet('/product/' + url).response({'name':'product1'})
var promise = mockFactory.getProduct('/product/' + url);
promise.then(function (prod) {
var products = prod;
});
$httpBackend.flush();
expect(products).toBeTruthy();
}));
当我运行考试的时候,我得到了
Error: Unexpected request: GET /product/undefined
Expected GET /product/12345
我不确定在我的情况下如何编写 http 请求。谁能帮我解决这个问题?谢谢!
您似乎想测试 productFactory
而不是控制器。我能看到的唯一测试是确保发出适当的 HTTP 请求。
describe('productFactory test', function() {
beforeEach(module('myApp'));
it('getProduct requests /product/:id', inject(function($httpBackend, productFactory) {
var id = 123;
$httpBackend.expectGET('/product/' + id);
productFactory.getProduct(id);
$httpBackend.flush();
}));
});
此外,您在 productFactory.getProduct
中有错别字。应该是$http.get
,不是http.get
.
假设您的工厂使用 cookie 服务来获取 ID。像这样...
.factory('productFactory', function($http, cookieService) {
return {
getProduct: function() {
return $http.get('/product/' + cookieService.getProductid());
}
};
});
然后您将像这样测试它(假设 jasmine v2)...
describe('...', function() {
var cookieService;
beforeEach(module('myApp', function($provide) {
$provide.value('cookieService',
cookieService = jasmine.createSpyObj('cookieService', ['getProductId']));
});
it('...', inject(function($httpBackend, productFactory) {
cookieService.getProductId.and.returnValue(123);
$httpBackend.expectGET('/product/123');
productFactory.getProduct();
expect(cookieService.getProductId).toHaveBeenCalled();
$httpBackend.flush();
}));
});
我正在尝试为我的 http 请求编写单元测试
在我的控制器中,我有类似的东西
$scope.test1 = function(url) {
productFactory.getProduct(url)
.then(function(products){
$scope.result = products;
})
}
在我单位厂档
angular.module('myApp').factory('productFactory', function($http) {
var factoryObj = {};
factoryObj.getProduct = function(url) {
return http.get('/product/' + url)
}
return factoryObj
})
在我的单元测试中,我不确定如何编写测试,因为 url 是动态的
describe('test here', function () {
var testCtrl, scope, httpBackend, mockFactory;
// Initialize the controller and a mock scope
beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_, _productFactory_) {
scope = _$rootScope_.$new();
httpBackend = _$httpBackend_;
mockFactory = _productFactory_;
testCtrl = _$controller_('testCtrl', {
$scope: scope
});
// the url is dynamic so I am not sure what to write here.
var url = 12345 <----dynamic product id
httpBackend.whenGet('/product/' + url).response({'name':'product1'})
var promise = mockFactory.getProduct('/product/' + url);
promise.then(function (prod) {
var products = prod;
});
$httpBackend.flush();
expect(products).toBeTruthy();
}));
当我运行考试的时候,我得到了
Error: Unexpected request: GET /product/undefined
Expected GET /product/12345
我不确定在我的情况下如何编写 http 请求。谁能帮我解决这个问题?谢谢!
您似乎想测试 productFactory
而不是控制器。我能看到的唯一测试是确保发出适当的 HTTP 请求。
describe('productFactory test', function() {
beforeEach(module('myApp'));
it('getProduct requests /product/:id', inject(function($httpBackend, productFactory) {
var id = 123;
$httpBackend.expectGET('/product/' + id);
productFactory.getProduct(id);
$httpBackend.flush();
}));
});
此外,您在 productFactory.getProduct
中有错别字。应该是$http.get
,不是http.get
.
假设您的工厂使用 cookie 服务来获取 ID。像这样...
.factory('productFactory', function($http, cookieService) {
return {
getProduct: function() {
return $http.get('/product/' + cookieService.getProductid());
}
};
});
然后您将像这样测试它(假设 jasmine v2)...
describe('...', function() {
var cookieService;
beforeEach(module('myApp', function($provide) {
$provide.value('cookieService',
cookieService = jasmine.createSpyObj('cookieService', ['getProductId']));
});
it('...', inject(function($httpBackend, productFactory) {
cookieService.getProductId.and.returnValue(123);
$httpBackend.expectGET('/product/123');
productFactory.getProduct();
expect(cookieService.getProductId).toHaveBeenCalled();
$httpBackend.flush();
}));
});