首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@angular-redux/store测试@select装饰器

使用@angular-redux/store测试@select装饰器
EN

Stack Overflow用户
提问于 2017-11-09 01:59:46
回答 2查看 1.8K关注 0票数 0

当在Angular中测试依赖于@select()装饰器的组件时,我使用MockNgRedux.getSelectorStub('myStoreProperty').next(testState)向订阅者发送新值,但是当我调用next函数时,它不会用新值触发订阅。

参见示例代码:

代码语言:javascript
复制
export class BasicInfoComponent {
    @select() application$: Observable<Application>;
    this.application$.subscribe((app: Application) => { 
        //... this code is never triggered.
    }

}

下面是测试设置

代码语言:javascript
复制
beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [BasicInfoComponent],
      imports: [
        NgReduxTestingModule,
        ReactiveFormsModule
      ],
      providers: [
       //... providers
      ],
      schemas: [NO_ERRORS_SCHEMA]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(BasicInfoComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    MockNgRedux.reset();
  });

  afterEach(() => {
    fixture.destroy();
  });
EN

回答 2

Stack Overflow用户

发布于 2017-11-09 01:59:46

在创建组件之前,必须调用MockNgRedux.reset()。MockNgRedux连接到测试期间创建的所有@select装饰器,在组件创建@select装饰器后调用reset将断开MockNgRedux与其当前连接的所有装饰器的连接。

beforeEach函数修改为如下所示:

代码语言:javascript
复制
beforeEach(() => {
    // NOTE: MockNgRedux Reset must happen before the creation of the component
    // otherwise it will reset the connections to the select decorators.
    MockNgRedux.reset();
    fixture = TestBed.createComponent(BasicInfoComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
票数 0
EN

Stack Overflow用户

发布于 2018-02-02 19:05:20

我也遇到过类似的问题,但这是因为我在@select(...)中使用了lambda选择。似乎MockNgRedux需要您使用与@select(...)使用的完全相同的选择器来调用getSelectorStub

所以,如果你有

代码语言:javascript
复制
const selectorStub = MockNgRedux.getSelectorStub(['status', 'text']);

然后,您还需要在组件中以相同的方式选择它:

代码语言:javascript
复制
@select(['status', 'text'])
readonly statusText: Observable<string>;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47186607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档