小编xio*_*tee的帖子

Angular 2 Testing - 异步函数调用 - 何时使用

在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

74
推荐指数
2
解决办法
3万
查看次数

Angular 2.0.0 - 测试"由模块导入'DynamicTestModule'"

我在测试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)

任何人都可以帮我解决这个问题吗?

unit-testing karma-jasmine angular

22
推荐指数
1
解决办法
2万
查看次数

Angular 2 测试 - 组件实例未定义

我在 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 组件“不应该运行”。我不知道是什么原因造成的。我在控制台中收到错误消息(请参见下文)。如代码中所示,我已经在每个之前的第二个实例上创建了组件实例,但它指出在第二个测试用例上运行时它是未定义的。我需要帮助。我真的很感激。提前致谢。

控制台出错

unit-testing karma-jasmine angular-cli angular

6
推荐指数
0
解决办法
4606
查看次数

Angular 2 - 孙子访问

如何访问孙组件?例如,我有一个grandparent.component.tsparent.component.tschild.component.ts。该child.component.ts在其模板按钮列表。parent.component.ts包含child.component.ts。该grandparent.component.ts包含parent.component.ts。我想禁用在该child.component.ts按钮grandparent.component.ts。我该怎么做呢?

angular

5
推荐指数
1
解决办法
6620
查看次数

Angular 2 - 文件上传访问

<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中执行此操作,因为我仍然需要处理文件中的数据(例如,检查有效的文件类型)?

file-upload typescript angular

3
推荐指数
1
解决办法
1308
查看次数

Angular 2 - 在依赖于其他服务的组件中模拟服务

如何模拟依赖于组件中的另一个服务的服务?请检查下面的代码。

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中的服务?请帮忙。谢谢你。

unit-testing karma-jasmine angular

1
推荐指数
1
解决办法
1110
查看次数