lundi 9 février 2015

How to resolve my unit test

I am trying to write a unit test for my http request


In my controller, I have something like



$scope.test1 = function() {
productFactory.getName()
.then(function(productsName){
$scope.result = productsName;
})
}


productFactory



angular.module('myApp').factory('productFactory', function($http) {
var factoryObj = {};
factoryObj.getName = function() {
var productType = 'new';
return getProductID(productType)
.then(function(product){
var url = product.url
return http.get(url, product.id)
})

}

var getProductID = function(productType) {
return http.get('/product', productType)
}

return factoryObj
})


In my unit test file



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
});
// I am not sure how to write my httpbackend here because I have two requests
// first request is getProductID and the second one is return http.get(url,product.id)
// the url is dynamic so I am not sure what to write here.

httpBackend.whenGet(url).response() // not sure what to write here


it('should get product name', function() {
scope.test1();
//I am not sure how to test the results
});
}));


My two problems are 1. not sure how to write two continuously http request test. 2. URL is dynamic and I don't know how to set up the test.


Thanks for your help!


Aucun commentaire:

Enregistrer un commentaire