1
votes

I'm trying to test that a logged in user with a particular role can change other user roles. I'm using meteor-jasmine with PhantomJS (but I have also tried the test against meteor-jasmine's default browser Chrome).

I'm creating both an admin user (admin roles are automatically assigned to the first user created) and a standard user in my fixutes to test on. Then, I log in the admin user in the beforeEach.

describe('Users with manage-user role', function() {

    beforeEach(function(done) {
        Meteor.loginWithPassword('[email protected]', '12345678', function(error) {
            console.log('Logged in as admin');
            done();
        });
    });

I log out the admin in afterEach.

    afterEach(function(done) {
        Meteor.logout(function() {
            console.log('Logged out');
            done();
        });
    });

Here in the test, I want to go to a route, add a role to the user, then check that the change is reflected in the database. The newUser._id I'm logging is undefined. I've also tried logging just newUser, but that is also undefined.

    it('should be able to change user roles', function(done) {
        Router.go('users');
        Tracker.afterFlush(function() {
            var newUser = Meteor.users.findOne({email: '[email protected]'});
            console.log('New user id: ' + newUser._id);
            $('#user-' + newUser._id + '-roles').val('manage-users');
            expect(Roles.userIsInRole(newUser, 'manage-users')).toBe(true);
            expect(Roles.userIsInRole(newUser, 'edit-any')).toBe(false);
            done();
        });
    });
});

I get an error for this test:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Jasmine test timeout error

Additionally, Chrome complains that newUser is undefined:

Exception from Tracker afterFlush function: Cannot read property '_id' of undefined

Why is my newUser variable undefined? Why does my test timeout? Is there a better way of testing this functionality?

1
Does Meteor.users actually contain the users you expect? Did it get solved by now? - Anima-t3d

1 Answers

-1
votes

Move Router.go() after the Tracker.afterFlush declaration.