Satyaki Mukherjee
Satyaki Mukherjee

Reputation: 2879

Jasmine unit test $state.go not working on test file

In my controller we have define the following two methods -

function goToHome() {
  $state.go('app.home', {newReleaseIds: vm.newReleaseIds});
}

function createAnotherFuelRelease() {
  // GA -- start creating another fuel release
  $analytics.eventTrack('Start creating another fuel release', {category: 'Iron', label: moment().format('MMMM Do YYYY, h:mm:ss a')});
  $state.go('app.create-iron', {selectedLocation: vm.selectedLocation, fuelReleaseNumber: vm.fuelReleaseNumber + 1, newReleaseIds: vm.newReleaseIds, pricing: $state.params.pricing});
}

called this two method from controller -

vm.createAnotherFuelRelease = createAnotherFuelRelease;
vm.goToHome = goToHome;

Now I would like to test those method from spec.js files -

it('should check goToHome()', function() {

 // spyOn($state, 'go');
 // $scope.inviteMembers(1);
 // expect($state.go).toHaveBeenCalledWith('invite', {deptId: 1});

 var spy = sinon.spy();
 scope.vm.goToHome = {goToHome : spy};
 scope.$digest();
 expect($state.go).toHaveBeenCalledWith('app.home', {newReleaseIds: 1});
 // expect(spy.calledOnce).toEqual(false);
 // $compiledElement.find('.nv-button.test-gotToHome').trigger('click');
 // expect(spy.calledOnce).toEqual(true);
 // spy.reset();

});

but it doesn't work. If anyone knows that stuff please let me know.

Upvotes: 0

Views: 296

Answers (1)

7uc4
7uc4

Reputation: 194

Here you can find an example. It works with $location service but the idea is the same. Replace $location with $state and be sure to load its module.

'use strict';

angular.module('test', []);
angular.module('test').controller('ctrl', function ctrlFactory($location) {
    var vm = this;
    vm.greet = function() {
        $location.path('foo');
    };
});

beforeEach(module('test'));

it('', inject(function($injector) {
    // Given
    var $controller = $injector.get('$controller');
    var $location = $injector.get('$location');
    var scope = $injector.get('$rootScope').$new();
    spyOn($location, 'path');
    var ctrl = $controller('ctrl', { $scope: scope });

    // When
    scope.$apply();
    ctrl.greet();

    //Then
    expect($location.path).toHaveBeenCalled();
}));

Upvotes: 0

Related Questions