Reputation: 2361
I am having below Controller in Angular JS
describe('Controller: homeCtrl', function () {
beforeEach(module('incident'));
var homeCtrl, $state;
beforeEach(inject(function ($controller, _$state_) {
$state = _$state_;
homeCtrl = $controller('homeCtrl', {$state: $state});
}));
it('to be defined', function () {
expect( homeCtrl).toBeDefined();
});
it('to be defined state', inject(function ($state) {
$state.expectTransitionTo('incidents');
}));
});
While writing spec for $state.go like below, it is giving error as per attached image:
describe('Controller: homeCtrl', function () {
beforeEach(module('incident'));
var homeCtrl, $state;
beforeEach(inject(function ($controller, _$state_) {
$state = _$state_;
homeCtrl = $controller('homeCtrl', {$state: $state});
}));
it('to be defined state', inject(function ($state) {
$state.expectTransitionTo('incidents');
}));
});
Any suggestion will be helpful or any sample code which shows how can i test this $state.go.
Upvotes: 1
Views: 1880
Reputation: 4588
You need to spy on state to make transition between states.here is the code
describe('Controller: homeCtrl', function () {
beforeEach(module('incident'));
var homeCtrl, $state;
beforeEach(inject(function ($controller, _$state_) {
$state = _$state_;
spyOn('$state','go'); spyOn($state, 'go');
// or
spyOn($state, 'go').andCallFake(function(state, params) {
// This replaces the 'go' functionality for the duration of your test
});
homeCtrl = $controller('homeCtrl', {$state: $state});
}));
it('to be defined state', inject(function ($state) {
$state.expectTransitionTo('incidents');
}));
});
Upvotes: 4