所以我是新來angularjs和它的嘲笑庫。我想測試一個特定的GET請求,但我總是得到這個錯誤的斷言第二,不能找出原因:AngularJS問題嘲笑httpGET請求
Error: Unsatisfied requests: GET /1.json
有什麼我搞砸了我下面的代碼?
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
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
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();
});
});
EDIT:添加的應用程序和控制器的代碼。
您的代碼表明您希望獲得「獲取」請求,但我沒有看到它在哪裏製作,這就是您遇到錯誤的原因。你在哪裏期待這個請求發生? –
抱歉,我忘記了包含應用程序和控制器代碼。我已經這樣做了,是我的錯誤發生可能是由於routeProvider?我需要告訴我的測試以某種方式執行路線嗎? – Lucas