jeudi 28 juillet 2016

Meteor testing angular helpers

I am trying to test some helper methods that I have in a controller. I am using mocha, chai, and sinon.

Here is the method I want to test:

isVisitor: ()=> {
  var user = User.findOne({_id: Meteor.userId()}, {fields: {'userData.role':           1}});
  if (user) {
    return user.userData.role === 'visitor';
  } else {
    return false;
  }
}

Here are the tests:

describe('feedback', function () {
var controller;
var findOneUserStub;

beforeEach(function () {
  findOneUserStub = sinon.stub(User, 'findOne');

  inject(function ($rootScope, $componentController) {
    controller = $componentController('feedback', {$scope: $rootScope.$new(true)})
  });
});

afterEach(function () {
  User.findOne.restore();
});

describe('Is a visitor?', function () {
  it('user is a visitor', function () {
    findOneUserStub.returns({userData: {role: 'visitor'}});
    chai.assert.equal(controller.isVisitor, true);
  });
  it('user is not a visitor', function () {
    findOneUserStub.returns({userData: {role: 'requester'}});
    chai.assert.equal(controller.isVisitor, false);
  })
});

The issue is it seems like the helper is fired off before I can set the return value. Does anyone know of a way to test this method?

Aucun commentaire:

Enregistrer un commentaire