【发布时间】:2017-06-07 06:10:33
【问题描述】:
我已经按照文档在我的 Angular 2 应用程序中成功实现了 Auth0 身份验证。我已经安装了 auth0-lock,并通过 npm install 为 auth0 和 auth0-lock 输入。
我的应用运行良好,成功注册并验证了用户。
但是,我的单元测试总是因“未定义 Auth0Lock”而失败。这是我的测试代码,它结合了另一个 SO 用户的建议,将 Auth0Lock 声明如下(无效):
/* tslint:disable:no-unused-variable */
import { TestBed, async, inject } from '@angular/core/testing';
import {AuthService} from "./auth.service";
import {RouterTestingModule} from "@angular/router/testing";
let Auth0Lock = require('auth0-lock').default;
describe('AuthService', () => {
let service: AuthService = null;
let router: any = {
navigate: jasmine.createSpy('navigate')
};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
AuthService,
{provide:RouterTestingModule, useValue: router}
],
imports: [RouterTestingModule]
});
});
beforeEach(inject([AuthService], (auth: AuthService) => {
service = auth;
}));
it('should exist', () => {
expect(service).toBeTruthy();
});
});
由于这个错误,我无法通过一个非常简单的测试来获得我需要测试的内容。
我尝试过从 auth0 导入 *,并从 auth0-lock 导入 *,但没有任何变化。
这是我的错误:
ReferenceError: Auth0Lock is not defined
at new AuthService (webpack:///./src/app/features/user/authentication/auth.service.ts?:21:25)
at DynamicTestModuleInjector.get (/DynamicTestModule/module.ngfactory.js:175:67)
at DynamicTestModuleInjector.getInternal (/DynamicTestModule/module.ngfactory.js:253:51)
at DynamicTestModuleInjector.NgModuleInjector.get (webpack:///./~/@angular/core/src/linker/ng_module_factory.js?:155:44)
at TestBed.get (webpack:///./~/@angular/core/bundles/core-testing.umd.js?:817:51)
at eval (webpack:///./~/@angular/core/bundles/core-testing.umd.js?:823:65)
at Array.map (native)
at TestBed.execute (webpack:///./~/@angular/core/bundles/core-testing.umd.js?:823:33)
at Object.eval (webpack:///./~/@angular/core/bundles/core-testing.umd.js?:911:49)
at ZoneDelegate.invoke (webpack:///./~/zone.js/dist/zone.js?:242:26)
at ProxyZoneSpec.onInvoke (webpack:///./~/zone.js/dist/proxy.js?:79:39)
at ZoneDelegate.invoke (webpack:///./~/zone.js/dist/zone.js?:241:32)
at Zone.run (webpack:///./~/zone.js/dist/zone.js?:113:43)
at Object.eval (webpack:///./~/zone.js/dist/jasmine-patch.js?:102:34)
fwiw, auth.service.ts?:21:25 包含以下内容:
lock = new Auth0Lock (mtmConfig.clientID, mtmConfig.domain, {
allowedConnections: ['Username-Password-Authentication'],
avatar: null,
theme: {
primaryColor: "#E79287",
foregroundColor: "#000000",
logo: "https://some real url/"
},
languageDictionary: {
title: "the title"
}
}
);
【问题讨论】:
标签: angular karma-jasmine auth0