how to properly use toHaveBeenCalled() for unit testing?
<p>I need to write a test that sets <em>state</em> then test to see if <em>stateManagerService</em> has been called. I'm getting the error:</p>
<blockquote>
<p>undefined is not an object (evaluating 'vm.stateManagerService.go')</p>
</blockquote>
<p>main.controller.js</p>
<pre><code>export class MainController {
    constructor($state, stateManagerService) {
        'ngInject';
        this.$state = $state; 
        if($state.current.name === 'main') {
            stateManagerService.go('list');
        }
    }

}
</code></pre>
<p>unit test main.controller.spec.js</p>
<pre><code>describe('controllers', () =&gt; {
    let vm;

    //
    beforeEach(inject(($controller, $state, stateManagerService) =&gt; {
        vm = $controller('MainController');

    }));

    it('should call stateManagerService when current name is main', ()=&gt; {
        vm.$state.current.name = 'main'
        console.log(vm.$state.current.name)
        expect(vm.stateManagerService.go()).toHaveBeenCalled();
    });
</code></pre>
<p>Also (slightly off-topic) I'm still confused as to why <em>this.$state = $state</em> is required. If left out, I get an error saying $state is undefined.</p>
<p>Update: Now getting the errors</p>
<blockquote>
<p>Error: spyOn could not find an object to spy upon for go() (line 1887)</p>
<p>TypeError: undefined is not an object (evaluating 'vm.stateManagerService.go')</p>
</blockquote>
<p>stateManager.service.js</p>
<pre><code>export class StateManagerService {
    constructor($state, $stateParams, platformDetectService){
        'ngInject';
        //deps
        this.$state = $state;
        this.$stateParams = $stateParams;
        this.isMobile = platformDetectService.isMobile;

        //internal
        this.lastStateName = null;
        this.lastStateParams = null;
    }

    //storage method for current state
    go (stateName, params){
        this.lastStateParams = this.$stateParams.page;
        this.lastState = this.$state.current.name.split('.')[1];



        let nextState = (this.isMobile) ? 'mobile.' + stateName : 'map.' + stateName;

        this.$state.go(nextState, params)
    }

    //returns last state or list
    goBack (){
        let lastState = this.lastState || 'list',
            page = this.lastStateParams || null;

        this.go(lastState, {page})
    }

}
</code></pre>