- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到的情况是有很多模拟的 http 请求。在进行 Angular 上传时,我的情况发生了一些可疑的事情。它总是抛出 status:200 success
和 html 完整正文响应
。
下面是我的 Angular 上传代码:
function fileUploadController(FileUploader) {
/* jshint validthis: true */
let vm = this;
vm.type = this.type;
vm.clientId = this.client;
let uploader = new FileUploader({
url: 'http://localhost:8001/prom-scenario-config/files?clientId=' + vm.clientId,
data: {type: vm.type}
});
vm.uploads = {uploader};
vm.upload = upload;
vm.uploads.uploader.queue = [];
vm.uploads.uploader.onCompleteItem = function (item, response) {
let type = item.uploader.data.type;
console.log('response => ', response);
};
}
httpbackend 代码的模拟如下所示:
$httpBackend.whenPOST(new RegExp('http://localhost:8001/prom-scenario-config/files\\?clientId=([a-zA-Z0-9-])$$'))
.respond(function () {
return [200, 'foo'];
});
但是对此没有影响。
我的正则表达式代码在构建时是否有任何错误?有或没有模拟代码。我收到的回复仍然是 200。
有太多的模拟请求,我很难确定正在调用哪个请求。
是否有任何棘手的方法来识别调用了哪个正则表达式调用?或者强制执行我的模拟请求?
以下是状态和响应的引用仅供引用。
最佳答案
单元测试假设一个单元是单独测试的。任何其他未经过测试的单元(即 Controller )都应该被模拟,尤其是第三方单元。
考虑到它是用 Jasmine 进行测试的,FileUpload
服务应该被 stub :
beforeEach(() => {
// a spy should be created inside beforeEach to be fresh every time
module('app', { FileUpload: jasmine.createSpy() });
});
然后逐行测试 Controller ,如下所示:
it('...', inject(($controller, FileUpload) => {
const ctrl = $controller('fileUploadController');
...
expect(FileUpload).toHaveBeenCalledTimes(1);
expect(FileUpload).toHaveBeenCalledWith({ url: '...', type: {...} });
// called with new
const fileUpload = FileUpload.calls.first().object;
expect(fileUpload instanceof FileUpload).toBe(true);
expect(ctrl.fileUpload).toBe(fileUpload);
expect(fileUpload.onCompleteItem).toEqual(jasmine.any(Function));
expect(fileUpload.queue).toEqual([]);
...
}));
在上面的代码中,clientId=([a-zA-Z0-9-])
regexp 部分仅匹配由单个字符组成的 id,这是不正确的。这就是为什么在单元测试中优于硬编码值的原因,人为错误更容易发现和检测。当测试太松散时,不可能明确地识别问题,这会导致时间的浪费。
关于javascript - 调试模拟 httpbackend 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43964631/
在阅读了$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(
我是一名优秀的程序员,十分优秀!