gpt4 book ai didi

javascript - 测试 $state.go 在 Angular JS 规范中给出错误

转载 作者:行者123 更新时间:2023-11-30 10:01:40 24 4
gpt4 key购买 nike

我在 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');
}));

});

enter image description here

任何建议都会有所帮助,或者任何示例代码都会显示我如何测试这个 $state.go。

最佳答案

您需要监视状态以在状态之间进行转换。这是代码

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');
}));

});

关于javascript - 测试 $state.go 在 Angular JS 规范中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com