試圖測試在茉莉花&噶和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