简体   繁体   中英

Angular, Subjects Unit Testing

We have recently implemented a message service largely based on this solution in Angular 7, but we are stucked on the unit testing implementation.

We have this service:

@Injectable()
export class MessageService {
  private subject = new Subject<any>();
  sendMessage(message: MessageInput) {
    this.subject.next(message);
  }
  clearMessage() {
    this.subject.next();
  }
  getMessage(): Observable<any> {
    return this.subject.asObservable();
  }
}

And we have this implemetation, listening for a message sent by another component:

ngOnInit() {
this.subscription = this.messageService.getMessage().subscribe(
  (message: MessageInput) => {
    if (message) {
      .....
    }
  }, () => {
      .....
  }
)};

We want to unit test our implementation, but we are not able to mock the sending and receiving of the message. We are trying this:

beforeEach(() => {
  TestBed.configureTestingModule({
    schemas: [NO_ERRORS_SCHEMA],
    declarations: [...],
    providers: [
      ...
      MessageService
    ],
    imports: [...]
  });
  fixture = TestBed.createComponent(...);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

it('makes ngOnInit expected calls', () => {
  const messageService: MessageService = fixture.debugElement.injector.get(
    MessageService
  );
  component.ngOnInit();
  expect(component.subscription).toBeDefined();
  const message: MessageInput = {text: TypeMessage...., data: '...'};
  const nextSpy = spyOn(messageService.getMessage(), 'subscribe');
  messageService.sendMessage(message);
  expect(nextSpy).toHaveBeenCalled();
});

We are a but lost there, could any one please help us? Thanks!

If you look at the source youll notice that whenever asObservable is called, a new object is created and returned. Because of that, you are basically spying on the wrong object.

You can takle this with the following refactoring:

@Injectable()
export class MessageService {
  private subject = new Subject<any>();
  public readonly messages$ = this.subject.asObservable();

  sendMessage(message: MessageInput) {
    this.subject.next(message);
  }
  clearMessage() {
    this.subject.next();
  }
  getMessage(): Observable<any> {
    return this.messages$;
  }
}

And update your test as follows:

it('makes ngOnInit expected calls', () => {
  const messageService: MessageService = fixture.debugElement.injector.get(MessageService);
  const nextSpy = spyOn(messageService.messages$, 'subscribe');

  component.ngOnInit();
  messageService.sendMessage(message); 

  expect(component.subscription).toBeDefined();
  expect(nextSpy).toHaveBeenCalled();
});

If your goal is to just mock the sending/receiving of the message (your service), you can just use a mock for the message service.

mockSubject = new Subject();
mockSubject =next({'Message'})

mockMessangerService= {
 getMessage:  jasmine.createSpy('openConfirmModal').and.returnValue(mockSubject.asObservable()),
 sendMessage: jasmine.createSpy('sendMessage')
}

TestBed.configureTestingModule({
 ...
 }).overrideComponent(*component*,
{   
  set: { providers: [{provide: MessageService , useValue: mockMessangerService}]}
}....

Then you can just test your spies

expect(mockMessangerService.getMessage).toHaveBeenCalled();
expect(mockMessangerService.sendMessage).toHaveBeenCalledWith('Message');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM