我是angular和jhipster的新手,我已经编辑了登录组件,添加了formbuilder和MatDialogRef,并更新了单元测试:
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatDialogRef } from '@angular/material/dialog';
/* Other imports */
@Component({
selector: 'jhi-login-modal',
templateUrl: './login.component.html',
styleUrls: ['login.scss']
})
export class JhiLoginModalComponent implements OnInit {
authenticationError: boolean;
hide = true;
loginForm: FormGroup;
constructor(
private readonly eventManager: JhiEventManager,
private readonly loginService: LoginService,
private readonly stateStorageService: StateStorageService,
private readonly router: Router,
private readonly fb: FormBuilder,
private readonly dialogRef: MatDialogRef<JhiLoginModalComponent>
) {}
ngOnInit(): void {
this.loginForm = this.fb.group({
username: ['', Validators.required],
password: ['', Validators.required],
rememberMe: true
});
}
/* Rest of code */
}
import {ComponentFixture, TestBed, async, inject, fakeAsync, tick} from '@angular/core/testing';
import {FormBuilder, ReactiveFormsModule, Validators} from '@angular/forms';
import {JhiEventManager} from 'ng-jhipster';
import {JhiLoginModalComponent} from 'app/shared/login/login.component';
import {StateStorageService} from 'app/core/auth/state-storage.service';
import {BatimentTestModule} from '../../../test.module';
import {MockLoginService} from '../../../helpers/mock-login.service';
import {MockStateStorageService} from '../../../helpers/mock-state-storage.service';
import {MatDialogModule, MatDialogRef} from '@angular/material/dialog';
describe('Component Tests', () => {
describe('LoginComponent', () => {
let comp: JhiLoginModalComponent;
let fixture: ComponentFixture<JhiLoginModalComponent>;
// create new instance of FormBuilder
const formBuilder: FormBuilder = new FormBuilder();
let mockLoginService: any;
let mockStateStorageService: any;
let mockRouter: any;
let mockEventManager: any;
let mockActiveModal: any;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [BatimentTestModule, ReactiveFormsModule, MatDialogModule],
declarations: [JhiLoginModalComponent],
providers: [
{
provide: LoginService,
useClass: MockLoginService
},
{
provide: StateStorageService,
useClass: MockStateStorageService
},
{
provide: FormBuilder,
useValue: formBuilder
},
{provide: MatDialogRef,
useValue: {}}
]
})
.overrideTemplate(JhiLoginModalComponent, '')
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(JhiLoginModalComponent);
comp = fixture.componentInstance;
console.log(comp);
comp.loginForm = formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required],
rememberMe: true
});
comp.ngOnInit();
fixture.detectChanges();
mockLoginService = fixture.debugElement.injector.get(LoginService);
mockStateStorageService = fixture.debugElement.injector.get(StateStorageService);
mockRouter = fixture.debugElement.injector.get(Router);
mockEventManager = fixture.debugElement.injector.get(JhiEventManager);
mockActiveModal = fixture.debugElement.injector.get(NgbActiveModal);
});
it('should authenticate the user upon login when previous state was set', inject(
[],
fakeAsync(() => {
// GIVEN
const credentials = {
username: 'admin',
password: 'admin',
rememberMe: true
};
comp.loginForm.patchValue({
username: 'admin',
password: 'admin',
rememberMe: true
});
mockLoginService.setResponse({});
mockStateStorageService.setResponse('admin/users?page=0');
// WHEN/
comp.login();
tick(); // simulate async
// THEN
expect(comp.authenticationError).toEqual(false);
expect(mockActiveModal.dismissSpy).toHaveBeenCalledWith('login success');
expect(mockEventManager.broadcastSpy).toHaveBeenCalledTimes(1);
expect(mockLoginService.loginSpy).toHaveBeenCalledWith(credentials);
expect(mockStateStorageService.getUrlSpy).toHaveBeenCalledTimes(1);
expect(mockStateStorageService.storeUrlSpy).toHaveBeenCalledWith(null);
expect(mockRouter.navigateByUrlSpy).toHaveBeenCalledWith('admin/users?page=0');
})
));
/* Other tests */
});
在构造函数中使用MatDialogRef,因此应该在spec.ts中添加provider
providers: [
{provide: MatDialogRef, useValue: {}},
{provide: MAT_DIALOG_DATA, useValue: []},
]
完整代码
import {MAT_DIALOG_DATA, MatDialogModule, MatDialogRef} from '@angular/material';
//...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [JhiLoginModalComponent],
imports: [
TranslateModule.forRoot(),
MatDialogModule,
BrowserDynamicTestingModule
],
providers: [
{provide: MatDialogRef, useValue: {}},
{provide: MAT_DIALOG_DATA, useValue: []},
]
})
.compileComponents();
}));
以下代码适用于ionic应用程序中的自定义闪屏组件,我已在其中请求服务器获取令牌和身份!使用“ionic角”:“^3.9.2” 现在,当收到身份响应时,想显示一个简单的警报!但根据API文档,我在SplashScreenComponent中注入了AlertController,在构建时,出现了以下错误!无法在internet上找到任何搜索解决方案。 AlertController错误!AlertC
从angular 4.4升级到5.0,并将所有HttpModule和Http更新到HttpClientModule后,我开始出现此错误。 我还再次添加了HttpModule,以确保这不是由于某些依赖关系造成的,但它并不能解决问题 应用程序内。模块,我已正确设置 我不知道这个错误是从哪里来的,或者我不知道如何去了解它。我也有一个警告(也放在下面),可能是相关的。 警告信息:
问题内容: 您好,我正在用Jasmine编写我的第一个角度测试,但是我一直收到错误消息 ------测试开始:文件:C:\ Users \ Regan \ Documents \ Visual Studio 2013 \ WebSites \ Regan \ testApp \ TestProject \ ng-tests \ MainCtrlSpec.js ------使用内联测试’MainCt
我正在使用Yeoman+Angular Generator为我的应用程序,我一直在努力地跑来跑去与Jasmine相处!这就是我被困住的地方。我希望能够在Jasmine测试中使用jQuery选择器。我已经安装了和包。然后我为bower安装了它(我是新来的,我不知道应该为什么安装什么!)。我已经在中手动添加了路径,但仍然得到这样的消息: 这就是我的因果报应配置的样子:
我有一个问题加载类到Angular组件。很长一段时间以来,我一直试图解决这个问题;我甚至尝试将它加入到一个文件中。我拥有的是: 一个pplication.ts 服务/名称服务。ts 我一直收到一条错误消息,说。 有人能帮我发现代码的问题吗?
我有一个自定义的十进制格式管道,它使用角十进制管道。这个管道是共享模块的一部分。我在功能模块中使用它,运行应用程序时没有出现提供者错误。 ./modules/shared.module.ts 我将自定义管道插入其中一个组件中,并调用transform方法来获得转换后的值。共享模块导入到功能模块中。