gpt4 book ai didi

javascript - Jasmine:在初始化时设置 Controller 属性, Angular 1.5

转载 作者:行者123 更新时间:2023-11-28 05:38:42 24 4
gpt4 key购买 nike

Controller

function SomeController(someService) {  const $ctrl = this;  activate();  function activate() {    someService.retrieveSomeData($ctrl.criteria).then(doSomething);  }  function doSomething(response) {    $ctrl.result = response.data;  }}

测试

describe('SomeController', () => {  beforeEach(module('myModule'));  let $controller;  beforeEach(inject((_$controller_,) => {    $controller = _$controller_;  })); it('tests $controller properties', () => {    const $scope = {};    const controller = $controller('SomeController', { $scope });  });});

在这里,我想在初始化 Controller 时发送 $ctrl.criteria ,以便之后可以测试 $ctrl.result

最佳答案

试试这个:

describe('SomeController', () => {

beforeEach(module('myModule'));

let $controller;

beforeEach(inject((_$controller_,) => {
$controller = _$controller_;
}));

it('tests $controller properties', () => {
const scope = {
criteria : {id: 1}
};
const controller = $controller('SomeController', { $scope : scope });
});
});

关于javascript - Jasmine:在初始化时设置 Controller 属性, Angular 1.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120625/

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