使用角度控制器中的vm进行Jasmine测试

时间:2015-10-23 22:28:45

标签: angularjs testing jasmine karma-jasmine

尝试在Jasmine&中测试一个简单的测试Karma和AngularJs框架工作, 控制器:

(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() {

    }
}
})();

AnyOne能告诉我如何使用Jasmin编写一个简单的测试用例吗?也许可能是一种方法及其测试, 我的测试代码如下:

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 - 等待...... 任何建议表示赞赏,我是Jasmin测试的新手。谢谢你

2 个答案:

答案 0 :(得分:0)

您实际上是在BoardPerformance块中实例化beforeEach控制器,但您显示的代码是Account,所以我理解这是一个错误。

因此,要运行简单的测试用例,请尝试以下方法:

describe('Account', function() {

  var scope, controller, userServiceMock;

  beforeEach(module('bsp'));

  beforeEach(function() {
    userServiceMock = {
      getUsername: function(){} 
    }
  });

  beforeEach(inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    controller = $controller('Account', {
      'userService': userServiceMock
    });
  }));

  it('checkTitle', function(){
    expect(controller.title).toEqual('Accounts'); 
  });

});

答案 1 :(得分:0)

所以这是Jasmin的新测试案例,

describe('Account', function() { 
  var scope, controller, userServiceMock,accountServiceMock; 
  beforeEach(module('bsp')); 
  beforeEach(function() {
    userServiceMock = {
       getUsername: function(){} //this is a method inside userService
    };
    accountServiceMock = {
      showPasswordModal :function(){}//this is a method inside accountService
    };
  });

  beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('Account', {
  'userService': userServiceMock,
  'accountService':accountServiceMock
});
  }));

 describe('testing Title',function(){
   it('checkTitle', function(){
   expect(controller.title).toEqual('Accounts'); 
   });  
 }); 
});

这是非常基本的,但对于具有依赖性的控制器以及对Pablo的vm感谢有必要的信息

相关问题