小编Meg*_*gan的帖子

如何在Angular 4中使用提供程序模拟组件?- 单元测试

我在使用Angular 4中的提供程序模拟组件时遇到问题.以下是代码:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/core/platform-browser';
import { DebugElement } from '@angular/core';
import { FormsModule,
    ReactiveFormsModule,
    FormBuilder
} from '@angular/forms';
import { Router, RouterModule } from '@angular/router';
import {
    Http, ConnectionBackend,
    BaseRequestOptions
} from '@angular/http';
import { MockBackend, async } from '@angular/http/testing';

import { LoginComponent } from './../../../src/app/login/login.component';
import { LoginService } from './../../../src/app/login/login.service';
import { LoginComponent } from './../../../src/app/login/login.component';
import { LoggerService } from './../../../src/app/logger-service';
import { AuthService } from …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocking jasmine angular

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

如何模拟@ ngrx/store?

我想对一个组件进行单元测试.我正在使用@ ngrx/store和rxjs来从我的组件更新我的商店.为此,我订阅并取消订阅商店.我的单元测试失败了

TypeError:undefined不是config/spec-bundle.js中的对象(评估'this.store.unsubscribe')

我无法找到如何正确模拟我的商店及其事件发射器.

以下是以下几行:

app.component.ts:

  ngOnDestroy() {
    // unsubscribe the observable
    this.userStore.unsubscribe();
  }
Run Code Online (Sandbox Code Playgroud)

app.component.spec.ts:

我怎么嘲笑商店:

        export class MockStore extends Subject<fromRoot.State> implements Store <fromRoot.State> {}
Run Code Online (Sandbox Code Playgroud)

我如何配置:

        TestBed.configureTestingModule({
        imports: [
            FormsModule,
            ReactiveFormsModule,
            StoreModule.provideStore(reducer)
        ],
        declarations: [LoginComponent],
        providers: [
            BaseRequestOptions,
            MockBackend,
            { provide: AuthService, useValue: authServiceStub },
            { provide: LoginService, useValue: loginServiceStub },
            { provide: LoggerService, useValue: loggerServiceStub },
            {
                provide: Http, useFactory: (backend: ConnectionBackend,
                    defaultOptions: BaseRequestOptions) => {
                    return new Http(backend, defaultOptions);
                }, deps: [MockBackend, BaseRequestOptions]
            },
            { provide: Router, useClass: …
Run Code Online (Sandbox Code Playgroud)

mocking jasmine rxjs ngrx

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

标签 统计

jasmine ×2

mocking ×2

angular ×1

ngrx ×1

rxjs ×1

unit-testing ×1