试图测试在茉莉花&噶和AngularJs框架, 控制器一个简单的测试:茉莉花测试与角控制器VM
(function() {
'use strict';
angular
.module('bsp.account')
.controller('Account', Account);
/* @ngInject */
function Account(userService, accountService) {
var vm = this;
vm.title = 'Accounts';
vm.username = userService.getUsername();
vm.showPasswordModal = accountService.showPasswordModal;
vm.showLogoutModal = accountService.showLogoutModal;
activate();
////////////////
function activate() {
}
}
})();
谁能告诉我我怎样写使用茉莉这个简单的测试用例代码?也或许可能梅索德和测试, 我的测试代码如下:
describe('Account', function() {
var scope,
controller;
beforeEach(module('bsp.account'));
//sample testCase--
it('testCheck', function() {
expect('helloWorld').toBe("helloWorld");
});
describe('Account', function() {
//injecting the scope&controller--
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('Account', {
$scope: scope
});
scope.vm = controller;
}));
});
it("checkTitle",function(){
var vm = controller("Account",{$scope:scope});
expect(vm.title).toEqual("Accounts");
});
});
因果报应,响应:
Running "karma:unit:run" (karma) task
[2015-10-23 15:11:03.542] [DEBUG] config - Loading config /vagrant/frontend/build/karma-unit.js
✗ checkTitle
TypeError: 'undefined' is not a function (evaluating 'controller("Account",{$scope:scope})')
at /vagrant/frontend/src/app/account/account.controller.spec.js:33
LOG LOG: undefined
PhantomJS 1.9.8 (Linux 0.0.0) LOG: undefined
LOG LOG: '** if *********'
PhantomJS 1.9.8 (Linux 0.0.0) LOG: '** if *********'
PhantomJS 1.9.8 (Linux 0.0.0): Executed 37 of 37 (1 FAILED) (0.134 secs/0.128 secs)
Warning: Task "karma:unit:run" failed. Use --force to continue.
由于警告中止。 在周五2015年10月23日15时11分04秒GMT-0700(PDT)已完成在10.849s - 等待... 任何建议表示赞赏,相信新的茉莉testing.Thank您
我真诚的道歉,只是在写很多不同控制器的测试用例之间丢失了,我已经更新了它。给我一个错误,TypeError:'undefined'不是一个n对象(评估'controller.title') – sg28
'bsp'模块是否包含'bsp.account'作为依赖项? – Puigcerber
是的,它确实有it.updated,它,但即使包括它后,给我错误✗checkTitle \t错误:[$ injector:unpr]未知提供者:userServiceProvider < - userService < - 帐户 \t http://错误.angularjs.org/1.3.20/$ injector/unpr?p0 = userServiceProvider%20%3C-%20userService%20%3C-%20Account \t at /vagrant/frontend/vendor/angular/angular.js:4031 \t在的getService(/vagrant/frontend/vendor/angular/angular.js:4178) \t在/vagrant/frontend/vendor/angular/angular.js:4036 \t类型错误: '未定义' 不是一个对象(计算' controller.title') \t at – sg28