使用ngxs进行单元测试以获取对象数组

时间:2019-02-04 14:57:09

标签: angular unit-testing ngxs

我想创建一个单元测试以使用ngxs提取用户列表,但是我不知道如何编写它。该文档不是专门针对它。如果有人可以帮助我。谢谢

到目前为止,这是我测试文件中的内容

user.state.spec.ts

import { async, TestBed } from '@angular/core/testing';
import { NgxsModule, Store } from '@ngxs/store';
import { UserState } from '@app/store/state/user.state';
import { GetUsers } from '@app/store/actions/user.actions';
import { User } from '@app/models/User';
import { UserService } from '@app/providers/user.service';
import { HttpClientModule } from '@angular/common/http';

describe('User', () => {
  let store: Store;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        NgxsModule.forRoot([UserState]),
        HttpClientModule
      ],
      providers: [UserService]
    }).compileComponents();

    store = TestBed.get(Store);
  }));

  describe('GET_USER_LIST', () => {
    it('should return list of user', () => {

      const action = new GetUsers();
      store.dispatch(action);

      store.selectOnce((state: UserState) => state.getUsers).subscribe(actual => {
        expect(actual).toEqual(User[]);
      })
    });
  });
});

user.state.ts

 @Action(GetUsers)
  getUsers({ getState, setState }: StateContext<UserStateModel>) {
    return this.userService.fetchUsers().pipe(
      tap(result => {
        const state = getState();
        setState({
          ...state,
          users: result
        });
      })
    );
  }

Movie.ts

export interface Movie {
  id: number;
  title: string;
  imageUrl: string;
  liked: boolean;
  favorited: boolean;
}

0 个答案:

没有答案