工厂存根不使用angularjs mocha和sinon

时间:2014-07-07 22:37:59

标签: angularjs unit-testing mocha sinon

我在测试我的控制器时遇到了使用sinon来存根工厂的问题。 我在angularjs上使用了mocha,chai和sinon。

基本上,我有一个教室工厂,我想要存根和间谍,而我正在测试我的控制器。

这是我的控制器代码:

angular.module('app')
    .controller('ClassroomsCtrl', function ($scope, Classrooms){

  function init(){
    $scope.classrooms = Classrooms.search();
  }

  init();

});

这是我的工厂代码:

angular.module('app').factory('Classrooms', ['$resource', function($resource) {
  return $resource('/classrooms/:id', {
    id: '@id'
  },
  {
    search: {
      method: 'GET',
      url: '/classrooms/search',
      isArray: true
    }
  });
});

这是我的控制器单元测试:

angular.module('MyAppMocks',[]).
  factory('Classrooms', function(){
    return {
      search: sinon.stub()
    }
  });


describe('Controller: ClassroomsCtrl', function () {

  var scope, Classrooms, controllerFactory, spy;

  function createController() {
    return controllerFactory('ClassroomsCtrl', {
      $scope: scope,
      Classrooms: Classrooms
    });
  }

  // load the controller's module
  beforeEach(module('app'));

  beforeEach(module('MyAppMocks'));


  beforeEach(inject(function($controller, $rootScope, _Classrooms_){
    scope = $rootScope.$new();

    Classrooms = _Classrooms_;

    controllerFactory = $controller;

  }));


  it('should call Classrooms.search', function(){
    createController();
    expect(Classrooms.search).to.have.been.called();
  });

});

当我运行我的规范时,我收到此错误:

✗ should call Classrooms.search
    TypeError: '[object Object]' is not a function (evaluating 'expect(Classrooms.search).to.have.been.called()')
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/test/spec/controllers/classrooms.js:43
        at callFn (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4338)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4331
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4728
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4819
        at next (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4653)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4663
        at next (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4601)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4625
        at done (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4300)
        at callFn (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4343)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4331
        at next (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4626)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4625
        at done (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4300)
        at callFn (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4343)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4331
        at next (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4626)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4625
        at done (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4300)
        at callFn (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4343)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4331
        at next (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4626)
        at /Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:4630
        at timeslice (/Users/tc/Documents/code/sandbox/edumatcher/ng-app/node_modules/mocha/mocha.js:5763)

似乎我已经将正确的教室工厂注入正确,但有些东西不能与sinon合作。任何建议将不胜感激。感谢

1 个答案:

答案 0 :(得分:3)

我不认为这是它,但如果你改变了

expect(Classrooms.search).to.have.been.called();

expect(Classrooms.search).to.have.been.called;

使用chai(BDD风格,fwiw)时注意到那些不是函数,只是属性。愚蠢的BDD。 :)