AngularJS问题嘲讽HTTPGET请求 [英] AngularJS Issues mocking httpGET request
问题描述
所以我是新来angularjs和嘲讽库。我想测试一个特定的GET请求时,但我总是得到这个错误的断言第二,不能找出原因:
so I'm new to angularjs and its mocking library. I am trying to test that a specific GET request is made, but I always get this error for the 2nd assertion and can't figure out why:
Error: Unsatisfied requests: GET /1.json
有什么我搞砸了我的低于code?
Is there anything I messed up with my code below?
App.js
var App = angular.module('App', []).config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
}).when('/Items', {
templateUrl: 'views/items.html',
controller: 'Ctrl'
}).otherwise({
redirectTo: '/'
});
}]);
Ctrl.js
Ctrl.js
function Ctrl($scope, $http, $filter) {
$scope.items = [];
$http.get('/1.json').success(function(data) {$scope.items = data.items;});
}
Ctrl.$inject = ["$scope","$http", "$filter"];
规格/ Ctrl.js
Spec/Ctrl.js
describe('Controller: Ctrl', function() {
var $httpBackend;
// load the controller's module
beforeEach(module('App'));
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
$httpBackend.whenGET('/1.json').respond('Response!');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
var Ctrl, scope;
// Initialize the controller and a mock scope
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
Ctrl = $controller('Ctrl', {
$scope: scope
});
}));
it('should initialize with 0 items', function() {
expect(scope.items.length).toBe(0);
$httpBackend.flush();
});
it('should make store request', function(){
var controller = scope.$new(Ctrl);
$httpBackend.expectGET('/1.json');
$httpBackend.flush();
});
});
修改:添加的应用和控制器code
EDIT: added app and controller code.
推荐答案
我终于得到了我的单元测试工作!主要是因为我调整我的应用程序更有意义和更模块化的。
I finally got my unit tests working! Mostly because I restructured my application to make more sense and be more modular.
我会尽力给的信息来帮助运行到这个旁边的人:
I'll try to give information to help the next person that runs into this:
第一个是我切换到使用$资源,而不是HTTP $。
first of was I switched to using the $resource instead of $http.
而不是注入$注射器,注入我的 $ httpBackend 的像这样:
instead of injecting $injector, I injected $httpBackend like so:
beforeEach(inject(function(_$httpBackend_, $rootScope, $route, $controller){
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('/path/to/api').respond([{id:1}]);
而不是引用Ctrl键作为一个字符串,我在实际的类传入
instead of referencing 'Ctrl' as a string, I passed in the actual class
控制= $控制器(按Ctrl,{
$适用范围:适用范围
});
成为
var ProductsCtrl = ['$scope', function($scope){ ... }];
Ctrl = $controller(ProductsCtrl, {
$scope: scope
});`
请确保您,如果您使用$资源引用角resources.js文件
Make sure you are referencing the angular-resources.js file if you are using $resources
我真的爱Angularjs;我认为它只是需要一些时间来回绕如何测试你的头。祝您好运那里的!
I'm really loving Angularjs; I think it just takes some time to wrap your head around how to test. Best of luck out there!
这篇关于AngularJS问题嘲讽HTTPGET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!