I am trying to write a unit test for my http request
In my controller, I have something like
$scope.test1 = function(url) {
productFactory.getProduct(url)
.then(function(products){
$scope.result = products;
})
}
In my unit factory file
angular.module('myApp').factory('productFactory', function($http) {
var factoryObj = {};
factoryObj.getProduct = function(url) {
return http.get('/product/' + url)
}
return factoryObj
})
in my unit test, I am not sure how to write the test because url is dynamic
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();
}));
When I run the test, I got
Error: Unexpected request: GET /product/undefined
Expected GET /product/12345
I am not sure how to write a http request in my case. Can anyone help me about it? Thanks!
productFactory?