Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
921 stars 167 forks source link

Testing methods managed by a role by Mocha #247

Closed MuyBien closed 4 years ago

MuyBien commented 7 years ago

Hi!

I have a method that can be accessed only by an admin. I want to test it with mocha. But every time I want to add my test user to the admin role, I have the error:

1) sessionDates methods "before all" hook:
Error: Role 'admin' does not exist.

What am I doing wrong? Do you have to subscribe to the roles in the test too? But how to do that?

Thanks for any answer or tips.

Here my test code:

if (Meteor.isServer) {

  describe('sessionDates', function () {

    describe('methods', function(){

      const userID = Random.id();
      const sessionDateID = Random.id();
      const sessionDateCancelledID = Random.id();
      const sessionAdminID = Random.id();

      before(function () {
        /// Code creating the object (session and sessionDate)
        Roles.setUserRoles( sessionAdminID, 'admin', sessionAdded._id ); // <- ERROR
      });

      describe("cancel", function(){

        it("cancel a sessionDate", function(done){
          const args = { sessionDateID: sessionDateID };
          cancelSessionDate._execute({ userId: sessionAdminID }, args);
          assert.isTrue(sessionDateAdded.cancelled === true);
          done();
        });

      }
   }
 }
}
mitar commented 4 years ago

You could use a hook before all tests to subscribe.