1

The following test passes whether my expect statement is

expect(propertyFactory.readProperty).should.have.been.called;

or

expect(propertyFactory.readProperty).should.have.not.been.called;

I've tried this with or without the sinon stub.

describe('pf.propertyModule', function () {

  var controller;
  var propertyFactory;

  beforeEach(module('pf.core'));
  beforeEach(module('pf.propertyModule'));

  beforeEach(inject(function(_propertyFactory_) {
    propertyFactory = _propertyFactory_;
    /*
    sinon.stub(propertyFactory, 'readProperty', function() {
      return {
        data: {
          property: 'xyz'
        }
      }
    });*/
  }));

  describe('readPropertyCtrl', function () {
    it('should have a passing first test', function () {
      expect(propertyFactory.readProperty).should.have.been.called;
    });
  });
});

UPDATE: Ok, so I got past the initial "non-error" by ensuring "sinon-chai" was installed, but now I'm getting the output: thrown: [Function] } is not a spy or a call to a spy!

UPDATE: Ok, so I got the previous error working by calling sinon.spy(). However, now I'm wondering how one would go about mocking a method that is expected to return a promise. I'm getting the error " TypeError: 'undefined' is not an object (evaluating 'propertyFactory.readProperty({id: $stateParams.id}).then') "

Updated code:

describe('pf.propertyModule', function () {

  var ctrl;
  var scope;
  var propertyFactory;

  beforeEach(module('pf.core'));
  beforeEach(module('pf.propertyModule'));

  beforeEach(inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    propertyFactory = {
      readProperty: sinon.spy()
    };
    ctrl = $controller('readPropertyCtrl as vm', {
      $scope: scope,
      propertyFactory: propertyFactory
    });
  }));

  describe('readPropertyCtrl', function () {
    it('should have a passing first test', function () {
      expect(scope.vm).to.be.defined;
      expect(propertyFactory.readProperty).should.have.been.called;
    });
  });
});

Code to test:

propertyFactory.readProperty({id: $stateParams.id}).then(getPropertySuccess);
function getPropertySuccess(response) {
  vm.loadedProperty = response.data.property;
}

Updated Code:

describe('pf.propertyModule', function () {

  var ctrl;
  var scope;
  var propertyFactory;

  beforeEach(module('pf.core'));
  beforeEach(module('pf.propertyModule'));

  beforeEach(inject(function($rootScope, $controller, $q, _propertyFactory_) {

    scope = $rootScope.$new();
    propertyFactory = _propertyFactory_;
    var deferred = $q.defer();
    var promise = deferred.promise;
    sinon.stub(propertyFactory, "readProperty").returns(promise);
    deferred.resolve({property: 'xyz'});

    ctrl = $controller('readPropertyCtrl as vm', {
      $scope: scope,
      propertyFactory: propertyFactory
    });
  }));

  describe('readPropertyCtrl', function () {
    it('should have a passing first test', function () {
      expect(scope.vm).to.be.defined;
      expect(propertyFactory.readProperty).should.have.been.called;
    });
  });
});

This attempt is returning thrown: [Function] } is not a spy or a call to a spy!

ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82

0 Answers0