- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试测试 http 调用的响应。问题是,无论我在那里输入什么响应代码或响应数据,它都会通过。有任何想法吗?谢谢!
Controller .js
$scope.getPresses = function() {
var pressRequest = {
method: 'GET',
url: localAPI.url + 'press/',
headers: requestHeaders
};
$http(pressRequest)
.success(function(data) {
$scope.userPresses = data.results;
})
.error(function() {
alert("We were not able to retrieve your press data");
});
};
testController.js
describe('Get Presses', function() {
it("sets scope attributes for the user's presses", function() {
$scope.getPresses()
$httpBackend.expectGET('http://0.0.0.0:8000/api/press/', function() {
return {
Authorization: "Token fakeToken2903920932"
}
}).respond(304, 'responseData')
$httpBackend.flush()
});
});
最佳答案
好吧,首先,避免像 alert
这样的全局函数。它们很难测试。相反,将 $window
注入(inject)您的 Controller 并使用
$window.alert("We were not able to retrieve your press data");
现在,对于您的测试,您需要测试实际结果。例如...
var $window, $scope, $httpBackend;
beforeEach(function() {
module('your.module', function($provide) {
$provide.value('$window', $window = jasmine.createSpyObj('$window', ['alert']));
});
// and however else you set up your $scope and $httpBackend vars
});
it('assigns results to userPresses on success', function() {
$httpBackend.expectGET('http://0.0.0.0:8000/api/press/', function(headers) {
return headers['Authorization'] === 'Token fakeToken2903920932';
}).respond({results: 'results'});
$scope.getPresses();
$httpBackend.flush();
expect($scope.userPresses).toBe('results');
});
it('calls alert on error', function() {
$httpBackend.whenGet('http://0.0.0.0:8000/api/press/').respond(500);
$scope.getPresses();
$httpBackend.flush();
expect($window.alert).toHaveBeenCalled();
});
关于javascript - Angular Jasmine $httpBackend 响应测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28489067/
在阅读了$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(
我是一名优秀的程序员,十分优秀!