当我尝试运行此测试用例时,有人可以告诉我我在这里做错了什么我得到一个错误: 错误:意外请求:GET" localhost:3000 / home?extended = true" 没有更多请求预期如何通过jasmine在一个控制器中处理多个HTTP和工厂调用?这是我的测试代码
describe('homeController Controller', function(){
beforeEach(module('myapp'));
var ctrl,$http,$httpBackend,scope,trendingProducts,newArrivalsProducts,store,categories,HomeProduct,Product;
beforeEach(inject(function(Product,HomeProduct,$injector,_$q_,$controller, $rootScope, $httpBackend,$http) {
var deferred = _$q_.defer();
Product = Product;
HomeProduct = HomeProduct;
$httpBackend = $injector.get('$httpBackend');
$http = $http;
trendingProducts = {};
newArrivalsProducts = {};
store = {};
categories = {};
trendingProducts = [];
rootScope = $rootScope;
scope = $rootScope.$new();
$httpBackend.when('GET','http://localhost:3000/home',{
'headers': {
'Content-Type': 'application/json'
},
'params': { extended: 'true' },
'withCredentials':true
}).respond({'success':true});
deferred.resolve('resolveData');
spyOn(Product,'fetchProductList').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchTrendingProduct').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchNewArrivals').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchStore').and.returnValue(deferred.promise);
spyOn(HomeProduct,'fetchCategories').and.returnValue(deferred.promise);
ctrl = $controller('homeController', {
$scope: scope,
trendingProducts:trendingProducts,
newArrivalsProducts:newArrivalsProducts,
store:store,
categories:categories
});
}));
it('Check Controller Promise function"s of Home Controller', function() {
rootScope.$apply();
});
});
答案 0 :(得分:1)
您已使用'http://localhost:3000/home'
嘲笑了您的$ httpBackend,但您已拨打'localhost:3000/home?extended=true'
- 没有'http://'
。
改善你的正则表达式。试试像:
$httpBackend.when('GET',/localhost:3000\/home/,...)
.respond({'success':true});