我正在尝试测试对我的API的简单调用,并且我会绕圈试图弄清楚它为什么会失败。
我已经简化了一些事情。
这将是以下测试的错误:
Error: Unexpected request: GET /api/search?blah=something
No more request expected
以下是测试:
it('does what it should', function() {
httpBackend.expectGET('/api/search?blah=something').respond(aTestResponse);
scope.search();
httpBackend.flush();
// expectations here...
});
控制器中的搜索功能:
function search() {
myDataService.getSearchResults().query(mySearchParams, function(response) {
// do stuff
}
}
和数据服务功能:
function getSearchResults() {
return $resource('/api/search', {
param1: '@param1',
param2: '@param2',
...etc
});
}
任何建议都会非常感激。
编辑 - 这是我的spec文件的一个经过编辑但更完整的版本:
'use strict';
describe('Controller: BlahCtrl', function() {
beforeEach(module('blahApp'));
beforeEach(module(function($urlRouterProvider) {
$urlRouterProvider.deferIntercept();
}));
var BlahCtrl;
var scope;
var rootScope;
var httpBackend;
beforeEach(inject(function($controller, $rootScope, $httpBackend) {
httpBackend = $httpBackend;
scope = $rootScope.$new();
rootScope = $rootScope;
BlahCtrl = $controller('BlahCtrl as vm', {
$scope: scope
});
this.testResults = [
{
testProperty1: 'test-value-1-1',
testProperty2: 'test-value-1-2',
testProperty3: 'test-value-1-3'
},
{
testProperty1: 'test-value-2-1',
testProperty2: 'test-value-2-2',
testProperty3: 'test-value-2-3'
}
];
}));
beforeEach(function() {
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});
it('stores the search results', function() {
httpBackend.expectGET('/api/search?blah=something').respond(this.testResults);
scope.vm.doSearch();
httpBackend.flush();
// expectations here...
});
});