gpt4 book ai didi

angular - 错误 : No provider for InjectionToken USE_DEFAULT_LANG

转载 作者:太空狗 更新时间:2023-10-29 18:30:14 28 4
gpt4 key购买 nike

我是 Angular 4 Jasmine 单元测试的新手。

  1. 请帮助我获得附加组件的完整测试范围。
  2. 我已经为翻译模块添加了子依赖项,但我仍然收到错误:没有 InjectionToken USE_DEFAULT_LANG 的提供者!解决这个问题的方法是什么???
  3. 我想知道如何在我附加的代码中覆盖变量和方法以实现完整覆盖。

感谢您的帮助。

登录组件:

   import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { HttpError } from '../core/http-error';
import { jsonFields } from '../app.constants';
import { LoggedUserData } from '../shared/model/loggedUserData';
import { Observable } from 'rxjs/Observable';
import { FormControl, Validators } from '@angular/forms';
import { LoginService } from '../shared/login.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
userID: string;
userPassword: string;
hide: boolean = true;
errorMsg: string;
errorShow: boolean = false;
showLoad: boolean = false;

constructor(private router: Router, private _http: Http, private _LoginService: LoginService) { }

loginFormControl = new FormControl('', [
Validators.required]);

ngOnInit() {
}
onLogin() {
this.showLoad = true;
var loggedUserData = new LoggedUserData();
var secPayload = {};
secPayload[jsonFields.login.userId] = this.userID;
secPayload[jsonFields.login.password] = this.userPassword;
secPayload[jsonFields.login.businessUnitId] = "7238";
secPayload[jsonFields.login.domainName] = "";

this._LoginService.authenticate(secPayload).subscribe(
result => {
loggedUserData.userId = result[jsonFields.loggedUserData.userId];
loggedUserData.token = result[jsonFields.loggedUserData.token];
loggedUserData.authTypeCode = result[jsonFields.loggedUserData.authTypeCode];
loggedUserData.businessUnitId = result[jsonFields.loggedUserData.businessUnitId];
loggedUserData.firstName = result[jsonFields.loggedUserData.firstName];
loggedUserData.lastName = result[jsonFields.loggedUserData.lastName];
loggedUserData.links = result[jsonFields.loggedUserData.links];
loggedUserData.locale = result[jsonFields.loggedUserData.locale];
loggedUserData.pwdExpired = result[jsonFields.loggedUserData.pwdExpired];
loggedUserData.securityId = result[jsonFields.loggedUserData.securityId];
loggedUserData.upn = result[jsonFields.loggedUserData.upn];
this.errorShow = false;
const token: string = loggedUserData.token;
if (token != null) {
localStorage.setItem("tokenId", token);
this.router.navigate(['delivery']);
}
else {
localStorage.removeItem("tokenId");
this.router.navigate(['login']);
}
this.showLoad = false;
},
(error: HttpError) => {
this.errorShow = true;
this.errorMsg = error.message;
this.showLoad = false;
}
);
}
}

登录组件规范:

import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';

import { LoginComponent } from './login.component';
import { LoginService } from '../shared/login.service';
import { MatProgressBarModule } from '@angular/material';
import { TranslateModule, TranslateService, TranslateLoader, TranslateFakeLoader, TranslateCompiler, TranslateParser, MissingTranslationHandler } from '@ngx-translate/core';
import { MatInputModule } from '@angular/material';
import { FormsModule } from '@angular/forms';
import { Router, RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { TranslationService } from '../translation/translation.service';
import { TranslateStore } from "@ngx-translate/core/src/translate.store";


describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;

const mockLoginService = {
authenticate() { },
validateLoginToken() { }
};

const mockTranslationService = {
validateLanguage() {
}
}

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [LoginComponent],
imports: [MatProgressBarModule, FormsModule, HttpModule, RouterModule, TranslateModule],
providers: [
{
provide: LoginService, useValue: mockLoginService
},
{
provide: TranslationService, useValue: mockTranslationService
},
{
provide: Router,
useClass: class { navigate = jasmine.createSpy("navigate"); }
},
{
provide: TranslateLoader,
useClass: TranslateFakeLoader
},
TranslateService, TranslateStore, TranslateCompiler, TranslateParser, MissingTranslationHandler
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
component.errorMsg = "dsadsad";
component.errorShow = true;
component.hide = true;
component.showLoad = true;
component.userID = '';
component.userPassword = '';


expect(component).toBeTruthy();
expect(component.onLogin).toBeTruthy();

});

it('validate Login', inject([LoginService],
(service: LoginService) => {
expect(service.authenticate).toBeTruthy();
expect(service.validateLoginToken).toBeTruthy();
}));

it('should translate the language service', inject([TranslationService],
(service: TranslationService) => {
expect(service).toBeTruthy();
expect(service.validateLanguage).toBeTruthy();
}));
});

翻译模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

// AoT requires an exported function for factories
export function HttpLoaderFactory(httpClient: HttpClient) {
return new TranslateHttpLoader(httpClient, "./assets/i18n/", ".json");
}
@NgModule({
imports: [
CommonModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
declarations: []
})
export class TranslationModule { }

最佳答案

在您的规范文件中,将此提供程序添加到 TestBed 的配置部分:

    beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [LoginComponent],
imports: [MatProgressBarModule, FormsModule, HttpModule, RouterModule, TranslateModule],
providers: [

{ provide: USE_DEFAULT_LANG }, // <== add this line

{
provide: LoginService, useValue: mockLoginService
},
{
provide: TranslationService, useValue: mockTranslationService
},
{
provide: Router,
useClass: class { navigate = jasmine.createSpy("navigate"); }
},
{
provide: TranslateLoader,
useClass: TranslateFakeLoader
},
TranslateService, TranslateStore, TranslateCompiler, TranslateParser, MissingTranslationHandler
]
})
.compileComponents();
}));

您还需要此导入:

import {USE_DEFAULT_LANG} from '@ngx-translate/core';

这应该添加到来自 @ngx-translate/core 的其他导入中

关于angular - 错误 : No provider for InjectionToken USE_DEFAULT_LANG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47712728/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com