我想在 Angular 5 应用程序中为 ngrx 4 状态存储运行一些集成测试。我的愿望是一次性测试调度 ngrx 动作、效果、webapi、模拟服务器和选择器。为效果器和减速器编写单独的测试将耗费时间,我需要偷工减料。我只需要知道我的一个页面何时失败而不是确切的位置。
我在以前的项目中成功地做到了这一点,同时将 redux 与 Angular 2 一起使用。这很容易设置。但是,使用 ngrx 我遇到了一些麻烦。运行. AccountsWebapi
_ AccountsEffects
_ TestBed
看起来我没有收到 webapi 的实例,而是得到了构造函数。同时,相同的 webapi 似乎在_AccountsService
.
account.service.spec.ts
describe("AccountsService - ", () => {
let accService: AccountsService;
beforeAll( ()=> {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
});
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({appReducer}),
EffectsModule.forRoot([AccountsEffects]),
],
declarations: [],
providers: [
{ provide: AccountsService, useValue: AccountsService },
{ provide: AccountsWebapi, useValue: AccountsWebapi },
// ... Other dependencies
]
}).compileComponents();
// Instantiation
let _AccountsUtilsService = TestBed.get(AccountsUtilsService);
let _store = TestBed.get(Store);
let _AccountsService = TestBed.get(AccountsService);
// ... Other dependencies
accService = new _AccountsService(
new _AccountsUtilsService(),
new _AccountsWebapi(),
_store,
// ... Other dependencies
);
}));
it("Sample Test", () => {
console.log(`Accounts SERVICE`, accService.accountsWebapi);
accService.getAccountAsync(1);
expect(1).toEqual(1);
});
account.effects.ts
@Injectable()
export class AccountsEffects {
constructor(
private actions$: Actions,
private store$: Store<AppState>,
private accountsService: AccountsService,
private accountsWebapi: AccountsWebapi,
) {
console.log('Accounts EFFECTS', this.accountsWebapi)
// Typing this line will instantiate the dependency...
this.accountsWebapi = new (this.accountsWebapi as any)()
}
控制台日志