gpt4 book ai didi

javascript - Jasmine 单元测试因 Controller 中的解析值而失败

转载 作者:行者123 更新时间:2023-11-30 12:14:43 24 4
gpt4 key购买 nike

在我开始之前,我遵循了这里的建议但没有成功:How can I test a controller with resolve properties in AngularJS?

我是我的 app.js,我有一个简单的决心:

var app = angular.module('myApp', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {

$stateProvider

.state('index', {
url: '/index',
templateUrl: 'html/home.html',
controller: 'HomeController',
resolve: {
homeInfo: function() {
return {
subtitle : 'Welcome to tables, ladders and chairs'
};
}
}
})

在我的 HomeController.js 中:

app.controller('HomeController', function($scope, srv1, srv2, homeInfo, $rootScope) {

$rootScope.$broadcast('brdSubtitle', homeInfo.subtitle);

});

我的测试:

describe('HomeController', function () {

var $rootScope, $scope, $controller, homeController;

beforeEach(module('myApp'));

beforeEach(inject(function (_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$controller = _$controller_;

homeController = $controller('HomeController', {'$rootScope': $rootScope, '$scope': $scope, homeInfo: { subtitle : 'Welcome to tables, ladders and chairs' }});
}));

it('should exist', function() {
expect(homeController).toBeDefined();
});

it('should have a subtitle', function() {
expect(homeController.homeInfo.subtitle.text()).toBe('Welcome to tables, ladders and chairs');
});
});

错误是:

TypeError: homeController.homeInfo is undefined in /path/to/specs.js

最佳答案

您应该尝试在 beforeEach 中模拟服务值,因为显然将值放在 $controller 参数中不起作用。

beforeEach(module(function($provide) {
$provide.value('homeInfo', {
subtitle : 'Welcome to tables, ladders and chairs'
});
}));

关于javascript - Jasmine 单元测试因 Controller 中的解析值而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32718427/

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