简体   繁体   中英

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)

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!

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