angular - 带有业力假 XmlHttpRequest 问题的角度测试
问题描述
我正在尝试使用标准角度测试工具(Karma 和 Jasmine)测试角度服务(基于 feathersJS)。
当我的服务对后端进行 XHR 调用时(使用 socket.io),我想模拟这些调用来测试服务。
我使用 sinon.js 库,它提供了创建假服务器的功能,这部分工作。
我的问题是,当我在测试中模拟 XHR 响应时,来自 webpack 的其他 XHR 请求试图加载“polyfill.js”。当我调用我的服务的身份验证方法时会发生这种情况,因此由于 webpack xhr 请求,承诺总是回退到 catch 代码中。
请参阅下面的详细信息,因此如果有人可以帮助解决此“问题”或提供有关如何在 Angular/jasmine/karma 测试中模拟 XHR 请求的其他建议
请注意,我认为在浏览器中模拟全局 xmlHttpRequest 对象可能很棘手......
谢谢 !!
细节:
完整的错误是:
错误:在 ZoneDelegate../node_modules/zone.js/dist/的 setTimeout ( http://localhost:9876/node_modules/@feathersjs/authentication-client/lib/passport.js?:120:1 ) 的套接字连接超时zone.js.ZoneDelegate.invokeTask ( http://localhost:9876/_karma_webpack_/polyfills.js:2883:31 ) 在 ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvokeTask ( http://localhost:9876/node_modules/zone.js/dist/zone-testing.js?:319:1 ) 在 ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask ( http ://localhost:9876/_karma_webpack_/polyfills.js:2882:36 ) 在 Zone../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:9876/_karma_webpack_/polyfills.js:2650:47)在./node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask(http://localhost:9876/_karma_webpack_/polyfills.js :2958:34 ) 在 ZoneTask.invoke ( http://localhost:9876/_karma_webpack_/polyfills.js:2947:48 ) 在计时器 ( http://localhost:9876/_karma_webpack_/polyfills.js:4516:29 )"
请注意,我的服务不会使这个 XHR ......
这是我在 jasmine 中的(非常简单的)测试规范:
import { AppLoggerService } from '../../logger/app-logger/service/app-logger.service';
import { fakeAsync, async, tick } from '@angular/core/testing';
import { mock, instance, when, deepEqual, verify } from 'ts-mockito';
import { FeathersjsBackendService } from './backend-feathers.service';
import { BackendConfigClass } from 'src/app/shared/models/backend-config.model';
import * as sinon from 'sinon'
describe('Feathers backend service', () => {
var MockLoggerService: AppLoggerService = null
var feathersBackendService: FeathersjsBackendService = null
const fakeURL: string = 'http://localhost/socket.io'
var feathersBackendServiceConfig: BackendConfigClass = { apiEndPoint: fakeURL }
var mockSocketServer: any = null;
// create fake websocket server
beforeAll(() => {
})
beforeEach( () => {
mockSocketServer = sinon.createFakeServer()
mockSocketServer.respondWith(/http:\/\/localhost\/socket\.io\/.*/, function (xhr, id) {
xhr.respond(200, {}, 'authenticate')
})
mockSocketServer.respondWith(/^\/_karma_webpack_.*/, function (xhr, id) {
xhr.respond(200, {}, 'webpack module')
})
MockLoggerService = mock(AppLoggerService)
feathersBackendService = new FeathersjsBackendService(instance(MockLoggerService), feathersBackendServiceConfig)
expect(feathersBackendService).not.toBeNull()
})
afterEach( () => {
mockSocketServer.restore();
})
it('#1 - Should mock authenticate', () => {
var isAuth: boolean = false;
feathersBackendService.authenticate({ strategy: 'anonymous' })
.then((user) => {
isAuth = true;
})
.catch((error) => {
let a = 0;
})
mockSocketServer.respond();
expect(isAuth).toBe(true);
})
})
解决方案
经过大量测试,我认为嘲笑 socket.io 是个坏主意。所以我在我的项目中嵌入了一个假服务器,为 Ajax/socket.io 查询提供静态响应。
推荐阅读
- php - 根据仅保留最新的值删除数组中的重复对象(基于时间戳的第二个值)
- python - Selenium 用一个扭曲的 Python 滚动到视图中
- python - python在clojure中的累积等价物
- java - Docker compose 无法在 java 和 mysql 容器之间建立连接
- python - 到达某一行后如何停止程序?
- python - python 3.8中的Tensorflow安装错误
- sql - DAX 使用 AVERAGE 计算滚动平均值
- c++ - 浮点类型表示
- python - SyntaxError: 无效的语法没有解决方案
- android - 如何让cordova、Java SDK和Android SDK协同工作?