模拟在角度服务构造函数中运行的BehaviorSubject

时间:2018-04-04 15:34:55

标签: angular unit-testing jasmine angular5

我有这个服务,它在其构造函数上订阅了BehaviorSubject。

@Injectable()
export class DirectoryService {
   directoryFilter$: BehaviorSubject<IDirectoryFilter> = new BehaviorSubject<IDirectoryFilter>(null);

   constructor(
       private gwApiService: GwApiService,
       private usersService: UsersService) {
       this.directoryFilter$.subscribe((filter) => {
       this.resetPaging();
       this.getDirectoryViaFilter();
    });
}

我正在尝试为此服务编写单元测试并收到此错误:

  

TypeError:无法读取属性&#39;订阅&#39;未定义的

这是单元测试文件:

describe('directory-service', () => {
    let directoryService: DirectoryService;
    const mockGwApiService = jasmine.createSpyObj('mockGwApiService', ['post']);
    const mockUserService = jasmine.createSpyObj('mockUserService', ['']);

    beforeEach(() => {
        directoryService = new DirectoryService(mockGwApiService, mockUserService);
    });

    it('DirectoryService should be created', () => {
        directoryService = new DirectoryService(mockGwApiService, mockUserService);
        expect(directoryService).toBeTruthy();
    });
});

如何模拟directoryFilter $ .subscribe?

0 个答案:

没有答案