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 getProductDetail(url)
}
var getProductDetail = 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.
httpBackend.whenGet(url).response() // not sure what to write here
}));
I am not sure how to write a http request in my case. Can anyone help me about it? Thanks!
Aucun commentaire:
Enregistrer un commentaire