我有一个像这样编码的Jasmine测试:
it ("should send correct message to server to get data, and correctly set up scope when receiving it", function(){
$httpBackend.when('GET', 'https://localhost:44300/api/projectconfiguration/12').respond(fakedDtoBase);
$routeParams.projectId=fakeId; // user asks for editing project
scope.$apply(function(){
var controller=controllerToTest(); // so controller gets data when it is created
});
expect(scope.projectData).toEqual(fakedDtoBase);
});
Run Code Online (Sandbox Code Playgroud)
它有点工作,但我收到错误:
Error: Unexpected request: GET views/core/main/main.html
No more request expected
at $httpBackend (C:/SVN/src/ClientApp/client/bower_components/angular-mocks/angular-mocks.js:1207:9)
at sendReq (C:/SVN/src/ClientApp/client/bower_components/angular/angular.js:7800:9)
at $http.serverRequest (C:/SVN/src/ClientApp/client/bower_components/angular/angular.js:7534:16)
(more stack trace)....
Run Code Online (Sandbox Code Playgroud)
我意识到我可以嘲笑其他所有电话.但是,让我说我不关心我的测试想要加载什么,因为它可能会调用其他一些东西.我怎样才能确保所有其他请求只是"无声地发生",也许只为其他一切提供一个虚拟响应?
我的范围内有一个函数可以在用户单击按钮时检索我的服务状态,或者触发某个事件并自动调用此函数.
这是我的功能,在我使用的控制器中定义:
$scope.getStatus = function() {
$http({method: 'GET', url: config.entrypoint + config.api + '/outbound/service/' + $scope.serviceId})
.success(function(data) {
$scope.errorMessage = '';
$scope.serviceAllGood = data;
})
.error(function() {
$scope.serviceAllGood = '';
$scope.errorMessage = 'We are experiencing problems retrieving your service status.';
});
}
Run Code Online (Sandbox Code Playgroud)
单元测试如下:
describe('SendServiceCtrl', function(){
var scope, ctrl, $httpBackend, configuration;
beforeEach(function () {
module('papi', 'ngUpload');
});
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, config) {
configuration = config;
$httpBackend = _$httpBackend_;
$httpBackend.expectGET(configuration.entrypoint + configuration.api + "/user/outboundstatus/").respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"allowed":false}});
scope = $rootScope.$new();
ctrl = $controller('SendServiceCtrl', {$scope: …Run Code Online (Sandbox Code Playgroud) 看来这是工作的解决方案,展示了如何一起工作$httpBacked http://jsfiddle.net/EgMpe/8/
但对于我的情况:
路线
app.config(['$routeProvider', function($routeProvider) { $routeProvider.
when('/', {templateUrl: 'partials/user-list.html'}).
Run Code Online (Sandbox Code Playgroud)
...
假货服务:
app.run(function($httpBackend) {
var users = [{"id":1,"name":"bob","email":"bob@bobs.com"}, {"id":2,"name":"bob2","email":"bob2@bobs.com"}]
$httpBackend.whenGET('/rest/users').respond(function(method,url,data) {
console.log("Getting users");
return [200, users, {}];
});
});
Run Code Online (Sandbox Code Playgroud)
..
真实服务:
services.factory('Users', function($resource){
return $resource('/rest/users', {}, {
get: {method: 'GET', isArray:true}
});
});
Run Code Online (Sandbox Code Playgroud)
我有错误的时候去我的"/"路由重定向我user-list.html页:
错误:意外请求:GET partials/user-list.html $ httpBackend不再需要请求.../mysite/public/angular/libs/angular-1.2.0/angular-mocks.js:1060:9)
问题1:是否httpBackend阻止做任何其他http要求?
我尝试使用passThrough方法让http命中真正的服务器端:
$httpBackend.whenGET(/^\/mysite\//).passThrough();
Run Code Online (Sandbox Code Playgroud)
但这没有用.