vendredi 27 mai 2016

How to test Meteor.users with Mocha

I have a function as follows:

if(Meteor.isServer) {
Meteor.methods({
    addUser: function (newUser) {
        check(newUser, { email: String, password: String });
        userId = Accounts.createUser(newUser);
        return userId;
    },
    getUser: function (userID) {
        check(userID, String);
        return Meteor.users.find({_id: userID}).fetch();
    }
});

And I am trying to test this function using Mocha:

if (Meteor.isServer) {
let testUser;
describe('Users', () => {
    it("Add User", (done) => {

        testUser = {email: 'test@test.test', password: 'test'};

        try {
            testUser._id = Meteor.call('addUser', testUser);
            console.log(Accounts.users.find({_id: testUser._id}).fetch());
            done();
        } catch (err) {
            assert.fail();
        }
    });

    it("Get user", (done) => {
        try {
            Meteor.call('getUser', testUser._id);
            done();
        } catch (err) {
            assert.fail();
        }
    });
});

And I know that the meteor call with 'addUser' works, because the console.log after that returns the user that I just made and the first test passes when I run it with "meteor test --driver-package practicalmeteor:mocha"

But then I come to the second testing part, where I try to get the user with the meteor call 'getUser', but then I get stuck:

'Cannot call method 'find' of undefined'

Now I know that the difference is that I use 'Meteor.users' instead of 'Account.users' to find the user, but I am totally in the dark what the difference is between these two. Should I replace all the Meteor.users method calls with Accounts.user method calls or not? How would you test this?

Aucun commentaire:

Enregistrer un commentaire