我正在使用NativeScript 6+和Angular 8+编写应用程序。我正在尝试使用业力和茉莉花为我的服务编写一些单元测试。我的单元测试抛出错误。
如何正确编写单元测试?我阅读并遵循了此网页上的指示,但仍然无法正常工作:https://docs.nativescript.org/angular/tooling/testing/testing
我遇到的错误不仅发生在其他服务中,也发生在其他服务中。
karma.config.js
// list of files / patterns to load in the browser
files: [
'src/tests/setup.ts',
'src/tests/**/*.spec.ts'
],
测试
import {
nsTestBedAfterEach,
nsTestBedBeforeEach,
nsTestBedRender
} from 'nativescript-angular/testing';
import { HttpInterceptorService } from '../app/core/interceptors/http-interceptor-service';
describe('HttpInterceptorService', () => {
beforeEach(nsTestBedBeforeEach([HttpInterceptorService]));
afterEach(nsTestBedAfterEach(false));
it('should be created', () => {
});
it('should have its functions defined', () => {
});
});
错误:
NativeScript / 13.3 (13.3; iPad): Executed 15 of 17 SUCCESS (0 secs / 0.018 secs)
NativeScript / 13.3 (13.3; iPad) undefined at line undefined FAILED
An error was thrown in afterAll
TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
at <Jasmine>
at file:///app/vendor.js:157139:46
at <Jasmine>
at file:///app/vendor.js:157341:57
at <Jasmine>
at file:///app/bundle.js:1389:28
at <Jasmine>
TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
at <Jasmine>
at file:///app/vendor.js:157139:46
at <Jasmine>
at file:///app/vendor.js:157341:57
at <Jasmine>
at file:///app/bundle.js:1389:28
at <Jasmine>
NativeScript / 13.3 (13.3; iPad): Executed 16 of 17 (1 FAILED) (0.084 secs / 0.018 secs)
TOTAL: 1 FAILED, 15 SUCCESS
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:132:0: NSUTR: this.error: An error was thrown in afterAll
TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.jasmine.js:1445:0
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:188:0
at <Jasmine>
at file:///src/main.ts:11:16
at <Jasmine>
TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.jasmine.js:1445:0
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:188:0
at <Jasmine>
at file:///src/main.ts:11:16
at <Jasmine>
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:120:0: NSUTR: completed test run.
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:120:0: NSUTR: completed test run.
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:124:0: NSUTR: completeAck
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:124:0: NSUTR: completeAck
NativeScript / 13.3 (13.3; iPad) ERROR
这是我的http拦截器中的代码段:
@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
constructor(
private httpLoaderService: HttpLoaderService,
private authentication: AuthenticationService,
private ssoAuthentication: SsoAuthenticationService,
private endpointHelper: EndpointHelperService,
private router: RouterExtensions
) {}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
它在名为“核心”的模块中
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { NativeScriptCommonModule } from 'nativescript-angular/common';
import { HttpInterceptorService } from './interceptors/http-interceptor-service';
import { HttpLoaderService } from './shared/http-loader.service';
import { NativeScriptHttpClientModule } from 'nativescript-angular/http-client';
@NgModule({
declarations: [],
imports: [
NativeScriptCommonModule,
NativeScriptHttpClientModule
],
providers: [HttpInterceptorService, HttpLoaderService],
schemas: [NO_ERRORS_SCHEMA]
})
export class CoreModule { }