0

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)

Chait
  • 1,052
  • 2
  • 18
  • 30
user1247395
  • 409
  • 1
  • 9
  • 20

1 Answers1

1

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!

Corentin
  • 21
  • 1
  • 3