首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError: reducerManager.addFeatures不是一个函数

TypeError: reducerManager.addFeatures不是一个函数
EN

Stack Overflow用户
提问于 2019-10-02 14:56:30
回答 2查看 1.9K关注 0票数 0

我试图用spec文件编写测试,但错误如下:

TypeError: Cannot read property 'loading' of undefined --既不能理解,也不能解决问题。

有人帮我吗?

这是我的spec.ts文件:

代码语言:javascript
运行
复制
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { ShellHomeComponent } from './shell-home.component';
import { StoreOne } from './../../models';
import { Store, select } from '@ngrx/store';
import { cold } from 'jasmine-marbles';
import { StoreModule } from '@ngrx/store';
import { reducer } from './../../state/reducer/reducer-storeOne';

describe('ShellHomeComponent', () => {

    let fixture: ComponentFixture<ShellHomeComponent>;
    let mockStore: MockStore<StoreOne>;
    let component: ShellHomeComponent;

    const loadingState = {
        loading: true,
        items: [{ name: '1' }]
    } as StoreOne;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [ ShellHomeComponent ],
            providers: [provideMockStore({ initialState: loadingState })]
        })
        .compileComponents();

        mockStore = TestBed.get(Store);

    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(ShellHomeComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });


    it('should display loading as true', () => {
        const expected = cold('a', { a: true });
        expect(component.loading).toBeObservable(expected);
    });

});

我正在收到的错误:

代码语言:javascript
运行
复制
expect(received).toEqual(expected) // deep equality

    - Expected
    + Received

      Array [
        Object {
          "frame": 0,
          "notification": Notification {
    -       "error": undefined,
    -       "hasValue": true,
    -       "kind": "N",
    -       "value": true,
    +       "error": [TypeError: Cannot read property 'loading' of undefined],
    +       "hasValue": false,
    +       "kind": "E",
    +       "value": undefined,
          },
        },
      ]

      44 |     it('should display loading as true', () => {
      45 |         const expected = cold('a', { a: true });
    > 46 |         expect(component.loading).toBeObservable(expected);
         |                                   ^
      47 |     });
      48 |
      49 | });

      at compare (node_modules/jasmine-marbles/bundles/jasmine-marbles.umd.js:379:33)
      at src/app/module1/shell/shell-home/shell-home.component.spec.ts:46:35

  console.warn node_modules/@ngrx/store/bundles/store.umd.js:608
    The feature name "storeOne" does not exist in the state, therefore createFeatureSelector cannot access it.  Be sure it is imported in a loaded module using StoreModule.forRoot('storeOne', ...) or StoreModule.forFeature('storeOne', ...).  If the default state is intended to be undefined, as is the case with router state, this development-only warning message can be ignored.

我的componet.ts文件:

代码语言:javascript
运行
复制
import { Component, OnInit, OnChanges } from '@angular/core';
import { StoreOne, Item } from './../../models';
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
import * as slices from './../../state';
import * as actions from './../../state/actions';

@Component({
    selector: 'app-shell-home',
    templateUrl: './shell-home.component.html',
    styleUrls: ['./shell-home.component.scss']
})
export class ShellHomeComponent implements OnInit {

    constructor(private store: Store<StoreOne>) {}

    items: Observable<Item[]>;
    loading: Observable<boolean>;

    ngOnInit() {

        this.store.dispatch(actions.Items());
        this.items = this.store.pipe(select(slices.getItems));
        this.loading = this.store.pipe(select(slices.getLoad));

        this.store.subscribe(state => console.log(state));
       //{ "storeOne": { "loading": true, "items": [ {"name": "1" }, { "name": "2"} ] }}

    }


}

更新

我的加载状态更新如下:

代码语言:javascript
运行
复制
const loadingState = { storeOne: { loading: true, items: [{ name: 1 }] } } as StoreOne;

但得到的错误如下:

代码语言:javascript
运行
复制
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    src/app/module1/shell/shell-home/shell-home.component.spec.ts:16:26 - error TS2352: Conversion of type '{ storeOne: { loading: boolean; items: { name: number; }[]; }; }' to type 'StoreOne' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
      Type '{ storeOne: { loading: boolean; items: { name: number; }[]; }; }' is missing the following properties from type 'StoreOne': loading, items

    16     const loadingState = { storeOne: { loading: true, items: [{ name: 1 }] } } as StoreOne;
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EN

回答 2

Stack Overflow用户

发布于 2019-12-26 17:34:51

就我而言,问题在于进口的结构。我用

代码语言:javascript
运行
复制
StoreModule.forFeature('name', nameReducer)

在模块中具有:

代码语言:javascript
运行
复制
StoreModule.forFeature('name2', name2Reducer)

代码运行良好,但当我测试它时,它会失败。

解决方案是从模块内部的测试声明中导入所需的内容。

票数 3
EN

Stack Overflow用户

发布于 2019-10-02 15:54:06

您的代码不必要地将技术用于单元测试和集成测试。正确的解决方案取决于测试的目标:

  • 如果您的目标是编写集成测试(用组件的活动依赖项测试组件),那么导入forRoot
代码语言:javascript
运行
复制
TestBed.configureTestingModule({
  declarations: [ ShellHomeComponent ],
  imports: [StoreModule.forRoot({ storeOne: reducer })]
  // no need to use mock store for an integration test, import your reducer using `forRoot`
})
  • 如果您的目标是编写单元测试(模拟存储)(我认为是这样),请提供MockStore
代码语言:javascript
运行
复制
TestBed.configureTestingModule({
  declarations: [ ShellHomeComponent ],
  providers: [provideMockStore({ initialState: loadingState 
  // no need to import the `StoreModule` in a unit test, `provideMockStore` will do all the setup
})

在AngularUP,我介绍了单元测试与集成测试以及MockStore。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58204101

复制
相关文章

相似问题

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