1
我正在使用Karma和Jasmine为AngularJS应用程序编写单元测试,并且在我的一个测试中验证数据时遇到了一些麻烦。在这个测试中,我有一个使用服务的组件控制器。在我的测试中,我调用了一个在该服务中设置值的方法。当我测试它是否设置正确时,它看起来好像没有。我是否正确设置了测试以便能够检查此问题?Angular中的其他服务测试值
与服务组件
angular.module('myApp').component('actions',{
templateUrl: '/app/components/actions/actions-template.html',
controller: ['action.service', '$location', function (actionService, $location) {
var self = this;
self.setActiveItem = function (item) {
actionService.activeItem = item;
};
}]});
我的测试
describe('action.component.tests', function() {
var actionListComponent;
var actionServiceTest;
beforeEach(inject(function ($injector, $componentController) {
actionListComponent = $componentController('actions');
actionServiceTest = $injector.get('action.service');
}));
describe('action.component method tests', function() {
it('Should set an active item in the action service', function() {
var item = {};
actionListComponent.setActiveItem(item);
expect(actionServiceTest.activeItem).toBe(item);
});
});});
该错误消息我得到的回复是,测试失败,因为我用于测试的item
不匹配目前在actionServiceTest.activeItem
中设置了什么。我可以使用控制台日志记录来确认,无论我在测试中使用什么项目,它都不会将其设置在actionServiceTest.activeItem
之内。