嗨,我是新手测试业力的Jasmine。我试图使用controller.$rootScope.$new();
创建一个新范围,或者甚至只是$rootScope.$new();
但我收到错误
TypeError: Cannot read property '$new' of undefined
以下是描述块切换选项按钮中的相关代码代码我收到错误(这是我为注入rootscope而添加的唯一代码)
fdescribe('Online Statements', () => {
const module = window.module;
const inject = window.inject;
beforeEach(module('wbbUiApp'));
describe('Controller', () => {
let scope;
let controller;
let injectibles;
let bindings;
const createController = userDetails => {
inject(($componentController,
$window,
$filter,
$rootScope) => {
// The same `$inject` array values listed in `account-activity.controller.js`
injectibles = {
$window,
$filter,
$rootScope
};
// The same bindings listed in `account-activity.component.js`
bindings = {
accountDetails
};
controller = $componentController('onlineStatements', injectibles, bindings);
});
};
describe('toggle options button', () => {
it('should set the scope value to the index', () => {
scope = controller.$rootScope.$new(); // problem here
const index = 1;
controller.toggleOptions(index)
expect(scope.activeRow).toEqual(index)
})
})
});
这可能很简单,因为我刚开始。提前谢谢。
答案 0 :(得分:1)
这应该有效:
describe('Online Statements', () => {
beforeEach(module('wbbUiApp'));
let controller;
beforeEach(()=>{
inject(($injector)=> {
const createController = userDetails => {
inject(($componentController) => {
let bindings = {
accountDetails
};
controller = $componentController('onlineStatements', {}, bindings);
});
});
describe('toggle options button', () => {
it('should set the scope value to the index', () => {
controller.$onInit()
const index = 1;
controller.toggleOptions(index)
expect(controller.activeRow).toEqual(index)
})
})
});
正如您所见,您不需要在您的情况下手动注入任何东西。只有在组件控制器中直接依赖时才应注入$scope
。
你有时可能想为$ window注入模拟。