user1247395
user1247395

Reputation: 419

Jasmine Mock for nested Object in Angulartics

I have an Angular 2 project. In that project I have imported the Angulartics npm package and injected it into one of my components. In my component I am making a single call that I need to mock for an existing unit test.

this.angulartics2.eventTrack.next({ action: 'Track my event'});

In my test spec file I have done the following:

  1. Added this in beforeEach:

    mockAngulartics2 = jasmine.createSpyObj<Angulartics2>('angulartics2', ['eventTrack']);
    
  2. Added this to providers:

    { provide: Angulartics2, useValue: mockAngulartics2 },
    

When I run my test, I get the following error back. What is the correct way to mock the angulartics2.eventTrack.next object?

TypeError: undefined is not a constructor (evaluating 'this.angulartics2.eventTrack.next({ action: 'Track my event' })') in config/spec-bundle.js (line 145931)

Upvotes: 0

Views: 1011

Answers (1)

Corentin
Corentin

Reputation: 21

It's complaining about the call to next only. You correctly created a spy object for eventTrack but not for next. So in between 1) and 2), you can do:

mockAngulartics2.eventTrack = jasmine.createSpyObj('angulartics2', ['next']);

I had an issue mocking Angulartics2 and you lead me on the right way, so, thanks!

Upvotes: 1

Related Questions