- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
按照angularJS $httpBackend的官方指南,我将进行此测试,但Karma会给我这个错误:Error: No pending request to flush !
at Function.$httpBackend.flush
测试
'use strict';
describe('profileCtrl', function () {
var scope, $httpBackend;
beforeEach(angular.mock.module('maap'));
beforeEach(angular.mock.inject(function($rootScope, $controller, _$httpBackend_){
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', 'profile').respond([{id: 1, name: 'Bob'}]);
scope = $rootScope.$new();
$controller('profileCtrl', {$scope: scope});
}))
it('should fetch list of users', function(){
$httpBackend.flush();
expectGET(scope.current_user.length).toBe(1);
expect(scope.current_user[0].name).toBe('Bob');
});
'use strict';
angular.module('maap').controller('profileCtrl', function($scope, UserService) {
$scope.current_user = UserService.details(0);
最佳答案
_$httpBackend_
没有要刷新的内容,因为您没有在测试中发出任何http请求。
您需要调用一些发出http请求的代码。
然后,一旦您的测试中某处发出了http请求,就可以调用flush
方法,以便为已发出的请求提供响应。
就像是:
it('should fetch list of users', function(){
// Do something that will make an http request
MyService.getAllUser(function ...) // for example
// Then provide a response for the http request that
// has been made when getAllUser was called
$httpBackend.flush();
// Check the expected result.
expect(something).toBe('Bob');
});
关于angularjs - Angular 模拟$ httpBackend没有给出任何刷新请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22226160/
在阅读了$httpBackend的AngularJS文档后,我还是不太清楚$httpBackend.expect和$httpBackend.when之间的区别,因为两者都是函数模拟 http 请求。
$httpBackend.whenGET('/restpath/api/v1/books') .respond({// some data}); 我收到以下错误 Error: Unexpected r
经过太多时间后,我终其一生都无法让这个示例正常工作。我正在关注 Ben Lesh 的 excellent guides在 Angular 中模拟 $http 请求,但由于某种原因,服务请求未发送。 我
我在开发 Feed (RSS) 应用程序时正在使用 AngularJS 单元测试。 Feed 可以获取远程 rss 数据并对其进行解析并保存解析的项目。为了测试 rss 获取,我使用 $httpBac
我有包含单元测试的 AngularJS 应用程序。我在检查用户身份验证的应用程序的 .run 部分中又添加了一个 http 请求。现在大约 60% 的测试失败了,因为他们发现了“意外的 http 请求
我有 Angular (1.2.x) 单元测试,我正在使用 ngMock。我的项目有一个依赖于 sinon.fakeServer 的夹具系统。对于我的单元测试,我更愿意使用它而不是 $httpBack
在我看来,我有加载动画,该动画一直显示到我收到来自 API 的响应。 //Displayed before we've received API response Loading ... //Disp
我刚刚开始通过 Jasmine 在 AngularJS 中编写我的第一个单元测试。 不知何故,我仍然不明白为什么我应该模拟 $httpBackend。为了弄清楚我还不清楚的地方,我会写一个小例子: 想
我正在尝试将 url 与正则表达式匹配。但是下面的正则表达式不匹配。 $httpBackend.whenGET('/^rest\/find-reservations\?.*/)').respond(f
whenGET 不是函数,但是当我输出 proxy 时var 我清楚地看到函数在那里 { when: [Function], whenGET: [Function], whenPUT:
我遇到的情况是有很多模拟的 http 请求。在进行 Angular 上传时,我的情况发生了一些可疑的事情。它总是抛出 status:200 success 和 html 完整正文响应。 下面是我的 A
我正在尝试测试我的程序是否正确处理从 http 请求中收到的错误消息,但我不知道如何使用 $httpBackend 来模拟它。鉴于以下情况: $httpBackend.expectGET(path).
我正在使用 HttpBackend 来模拟我的 Angular 应用程序正在进行的某些调用的一些 Http 响应。 但是,当我运行测试时,我收到错误“意外请求:[object Object] 未定义”
我正在尝试围绕我的 Angular 项目构建一些 jasmine 单元测试。我遇到了一种情况,我不确定如何最好地解决。 我使用了一个响应拦截器,它能够在确定错误是 401 错误时重试请求。它将调用更新
我刚刚开始深入研究 AngularJS 的一些测试,但我不能说我完全理解它。我想在我的工厂中测试 REST API 方法,但我只是遇到错误。不知道我做错了什么。谁能帮帮我? 我的工厂: 'use st
我有这个 URL 可以匹配: $httpBackend.whenGET('/api/alerts/1121212156/0/4/0').repond(someObject) 问题是 112121215
我正在使用 Karma 对我的 AngularJS 应用程序进行单元测试,并使用 $HttpBackend 对后端进行 stub 。不知何故,flush() 方法似乎并没有解决我的所有请求,因为 Co
我正在开发一个 Angular 应用程序。我的应用程序运行时只有很少的 Controller 和服务。现在我想开始对服务和 Controller 进行单元测试。但是不知道我做错了什么,我什至无法为一项
我正在尝试为我正在使用 Angular JS 构建的应用程序模拟后端。 我有以下代码来模拟成功的登录请求: // Login $httpBackend.when('PUT','/v1/token/',
我有一个 $resource 用于处理实体的 crud http 请求。它包含在我的自定义服务中,目前不添加任何功能,所以不要介意它 - 它只是旧的普通 $resource: app.factory(
我是一名优秀的程序员,十分优秀!