测试 $state.go 在 Angular JS 规范中给出错误
Testing $state.go giving error in Angular JS spec
我在 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');
}));
});
像下面这样为 $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.
expectTransitionTo
不是 ui-router 自带的功能。我假设您是从 this 要点看到的,在这种情况下,您需要使用该要点并设置 stateMock
模块。
您需要监视状态以在 states.here 之间进行转换
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');
}));
});
我在 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');
}));
});
像下面这样为 $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.
expectTransitionTo
不是 ui-router 自带的功能。我假设您是从 this 要点看到的,在这种情况下,您需要使用该要点并设置 stateMock
模块。
您需要监视状态以在 states.here 之间进行转换
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');
}));
});