Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 5-如何测试依赖异步服务成功创建的应用程序组件?_Angular_Unit Testing_Karma Jasmine_Angular5_Angular Test - Fatal编程技术网

Angular 5-如何测试依赖异步服务成功创建的应用程序组件?

Angular 5-如何测试依赖异步服务成功创建的应用程序组件?,angular,unit-testing,karma-jasmine,angular5,angular-test,Angular,Unit Testing,Karma Jasmine,Angular5,Angular Test,当AppComponent依赖于具有promise构造函数的服务时,如何测试成功创建的AppComponent?在app.spec.ts中放置done语句的位置 App.spec.ts describe("AppComponent", () => { var fixture: ComponentFixture<AppComponent> = null; var cmp: AppComponent = null; beforeEach(done =>

当AppComponent依赖于具有promise构造函数的服务时,如何测试成功创建的AppComponent?在app.spec.ts中放置done语句的位置

App.spec.ts

describe("AppComponent", () => {
    var fixture: ComponentFixture<AppComponent> = null;
    var cmp: AppComponent = null;

    beforeEach(done => {
        OidcService.oidcStartup(() => {
            TestBed.configureTestingModule({
                imports: [AppModule],
                providers: [
                    { provide: APP_BASE_HREF, useValue: "/" },
                    Broadcaster
                ]
            }).compileComponents();
            done();
        });
    });

    beforeEach(async(() => {
        this.fixture = TestBed.createComponent(AppComponent);
        this.cmp = this.fixture.debugElement.componentInstance;
    }));

    it(
        "should create the app",
        done => {
            expect(this.cmp).toBeTruthy();

        },
        60000
    );
});
存储服务

@Injectable()
export class StorageService {
    public tenantID: string = null;
    constructor(private base64Svc: Base64Service) {
        // NOOP
        var self = this;
        OidcService.userMgr.getUser().then(user => {
            self.tenantID = user.profile.tenant_id;
        });
    }
...
}

我建议您模拟这项服务。 您可以使用测试床Api模拟它,如下所示:

模拟对象/类,例如:

let storageMock = {}
然后在试验台上使用它:

TestBed.configureTestingModule({
    imports: [AppModule],
    providers: [
        { provide: APP_BASE_HREF, useValue: "/" },
        Broadcaster,
        { provide: StorageService, useValue: storageMock}
    ]
}).compileComponents();
TestBed.configureTestingModule({
    imports: [AppModule],
    providers: [
        { provide: APP_BASE_HREF, useValue: "/" },
        Broadcaster,
        { provide: StorageService, useValue: storageMock}
    ]
}).compileComponents();