mercredi 14 novembre 2018

Mock observable from mocked injected service

I'm new to testing and I'm already lost.

I have a service, which I want to test:

animation.service.ts

...
public constructor(frameStateService: FrameStateService) {
     frameStateService.changedAvailableFrames
            .pipe(
                ...
                // Some code in pipe
                ...
            )
            ).subscribe(() => {
                ...
                // Some code in subscribe
                ...
            }
        )
}

public start(): void {

    this.animationStateService.changeStatus(AnimationStatus.Preloading);
    this.preloaderService.preloadAllFrames()
        .subscribe(
            () => {
                this.run();
            }
        );
}
...

The injected service:

frame-state.service.ts

@Injectable()
export class FrameStateService {

    private changedAvailableFramesSubject: Subject<LayerId> = new Subject();
    public changedAvailableFrames: Observable<LayerId> = this.changedAvailableFramesSubject.asObservable();

    public constructor() {}
...

In my tests I want to mock the changedAvailableFrames from FrameStateService.

What I already did:

animation.service.spec.ts

describe("AnimationService", () => {

    let animationService: AnimationService;

    const frameStateService = jasmine.createSpyObj("FrameStateService", ["someMethod"]);

    beforeEach(() => {

        animationService = new AnimationService(frameStateService);

    });

    it("Should do something", () => {

    // Arrange
    frameStateService.changedAvailableFrames.and.returnValue(of()); // This doesn't work
    spyOn(AnimationStateService.prototype, "changeStatus").and.returnValue(AnimationStatus.Running);

    // Act
    animationService.start();

    // Assert
    expect(AnimationStateService.prototype.changeStatus).not.toHaveBeenCalled();
   });

})

The error what I got:

TypeError: Cannot read property 'pipe' of undefined

It's obvious, because I doesn't define changedAvailableFrames, but I don't know how I should.

Aucun commentaire:

Enregistrer un commentaire