我在Angular JS下面有Controller
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');
}));
});
在编写$ state.go的规范时,如下图所示:
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');
}));
});
任何建议都会有所帮助或任何示例代码显示我如何测试这个$ state.go。
答案 0 :(得分:4)
您需要监视状态以在状态之间进行转换。这是代码
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');
}));
});
答案 1 :(得分:0)
expectTransitionTo
不是ui-router天生就有的功能。我假设您是从this gist看到的,在这种情况下,您需要使用该要点并设置stateMock
模块。