因此,我有一个简单的,经过淡化处理的应用程序,该应用程序返回北约字母,然后使用模拟和承诺对它进行单元测试。

HERE'S A LIVE DEMO

我正在尝试从我的spyOn.service()MainModel一个函数。在控制器中,我有一个名为natoAlphabet的延期承诺,该承诺成功显示在页面上。

最初,我是从getNato引用MainController的,但是我从未将MainController.getNato设置为MainModel.getNato

所以我添加了MainController

this.getNato = MainModel.getNato;


我收到以下错误:Expected spy getNato to have been called.

但是,在console log中,如果执行mockMainCtrl的控制台输出,则在顶部附近的beforeEach中模拟了控制器,您将得到Object {name: "Hello World!", getNato: Promise}

然后在第一个it()测试中的下面,输出为Object {name: "Hello World!"},但是,如果展开该测试,则会得到:

Object {name: "Hello World!"}
    getNato: Promise
    name: "Hello world!";
    __proto__: Object


beforeEach中的那个是getNato

我的错误

我的错误发生在Jasmine测试运行时,我从Spec.js的Expected spy getNato to have been called.行获得expect(mockMainCtrl.getNato).toHaveBeenCalled();

那我在做什么错?

我认为app.js没什么问题,因为该页面可以成功读取承诺。

附录:

theSpec.js:

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

  beforeEach(module('app'));

  var $scope, $q, mockMainCtrl, $controller, scope, deferred;

  beforeEach(inject(function($controller, _$rootScope_, _$q_, MainModel) {
    $q = _$q_;
    $scope = _$rootScope_.$new();

    deferred = _$q_.defer();

    mockMainCtrl = $controller('MainCtrl', {
      $scope: $scope,
      MainModel: MainModel
    });

    console.log(mockMainCtrl);
  }));

  it('spied and have been called', function() {
    spyOn(mockMainCtrl, 'getNato');
    console.log(mockMainCtrl);
    expect(mockMainCtrl.getNato).toHaveBeenCalled();
  });

  it('Name from service, instantiated from controller, to be mocked correctly', inject(function() {
    expect(mockMainCtrl.name)
      .toEqual("Hello World!");
  }));

  it('Get [getNato] mocked deferred promise', function(mainCtrl) {
    deferred.resolve([{ id: 1 }, { id: 2 }]);
    $scope.$apply();
    expect($scope.results).not.toBe(undefined);
    expect($scope.results).toEqual(['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India']);
    expect($scope.error).toBe(undefined);
  });
});


app.js:

var app = angular.module('app', []);

app.service('MainModel', function($q) {
  this.name = "Hello World!";

  var getNato = function() {
    var deferred = $q.defer();
    var theNatoAlphabet = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India'];
    deferred.resolve(theNatoAlphabet);
    return deferred.promise;
  };

  this.getNato = getNato();
});

app.controller('MainCtrl', function($scope, MainModel) {
  this.name = MainModel.name;

  var self = this;

  MainModel.getNato.then(function(data) {
    self.natoAlphabet = data;
    $scope.results = data;
  }).catch(function() {
    $scope.error = 'There has been an error!';
  });

  this.getNato = MainModel.getNato;


});

最佳答案

看看-http://plnkr.co/edit/57ZA8BXscmdY6oDX5IOA?p=preview

您需要在此处'spyOn'依赖关系,即'MainModel',并在'$ controller'构造之前执行此操作,因为在控制器的构造上解决了'promise'。希望这可以帮助。

就像是 -

  beforeEach(inject(function($controller, _$rootScope_, _MainModel_) {
    scope = _$rootScope_.$new();
    MainModel = _MainModel_;
    spyOn(MainModel, 'getNato').andCallThrough();
    mockMainCtrl = $controller('MainCtrl', {
      $scope: scope
    });
  }));

  it('spied and have been called', function() {
    expect(MainModel.getNato).toHaveBeenCalled();
  });

关于javascript - AngularJS-将函数传递给spyOn,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34159952/

10-13 04:47