在Angular 2中进行测试时,何时在TestBed中使用异步函数?
你什么时候用的?
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MyModule],
schemas: [NO_ERRORS_SCHEMA],
});
});
Run Code Online (Sandbox Code Playgroud)
你何时使用它?
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyModule],
schemas: [NO_ERRORS_SCHEMA],
});
}));
Run Code Online (Sandbox Code Playgroud)
任何人都可以启发我吗?
unit-testing karma-jasmine angular2-testing angular angular-test
我在测试Angular 2中的app.component.ts时遇到问题.我正在使用angular-cli.每当我运行测试时,我的app.component.spec.ts会使控制台提示错误:
Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
Run Code Online (Sandbox Code Playgroud)
我在TestBed中导入了HomeModuleComponent
TestBed.configureTestingModule({
declarations: [AppComponent],
imports : [ HomeModuleComponent ]
});
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗?
我在 Angular 2 中进行测试时遇到问题。
describe('Spinner Component', () => {
beforeEach(() => TestBed.configureTestingModule({
declarations: [SpinnerComponent]
}).compileComponents());
beforeEach(() => {
fixture = TestBed.createComponent(SpinnerComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
});
it('Should Create a Spinner Component', () => {
fixture.detectChanges();
var compiled = fixture.debugElement.nativeElement;
expect(compiled).toBeTruthy();
});
it('Should not be running', () => {
fixture.detectChanges();
expect(comp.isRunning).toBe(false);
});
});
Run Code Online (Sandbox Code Playgroud)
上面的代码显示测试中的 Spinner 组件“不应该运行”。我不知道是什么原因造成的。我在控制台中收到错误消息(请参见下文)。如代码中所示,我已经在每个之前的第二个实例上创建了组件实例,但它指出在第二个测试用例上运行时它是未定义的。我需要帮助。我真的很感激。提前致谢。
如何访问孙组件?例如,我有一个grandparent.component.ts,parent.component.ts和child.component.ts。该child.component.ts在其模板按钮列表。parent.component.ts包含child.component.ts。该grandparent.component.ts包含parent.component.ts。我想禁用在该child.component.ts按钮grandparent.component.ts。我该怎么做呢?
<div class="fileUpload btn btn-primary">
<span>Choose File</span>
<input id="uploadBtn" type="file" class="upload" value="No File Selected" #uploadBtn/>
</div>
<input id="uploadFile" placeholder="No File Chosen" disabled="disabled" value="{{uploadBtn.value}}"/>
Run Code Online (Sandbox Code Playgroud)
如何访问用户上传的文件?我如何在angular和typescript中执行此操作,因为我仍然需要处理文件中的数据(例如,检查有效的文件类型)?
如何模拟依赖于组件中的另一个服务的服务?请检查下面的代码。
a.component.ts
@Component({
selector: 'my-comp',
templateUrl: './my.component.html',
providers: [ MyServiceA ]
})
export class MyComponent {
Run Code Online (Sandbox Code Playgroud)
我的服务-a.service.ts
@Injectable()
export class MyServiceA{
constructor(private myServiceB: MyServiceB) {}
Run Code Online (Sandbox Code Playgroud)
我的服务-b.service.ts
export class MyServiceB{
constructor(private myServiceC: MyServiceC,
private myServiceD: MyServiceD) {}
Run Code Online (Sandbox Code Playgroud)
如何在 TestBed 配置中模拟a.component.spec.ts中的服务?请帮忙。谢谢你。