jasmine unit testing with return value of created user

242 views
Skip to first unread message

Ustag

unread,
Aug 6, 2014, 3:57:58 AM8/6/14
to meteo...@googlegroups.com
I am trying to write a test for creating a user and controlling that the id of created user is being returned.

I have a simple method on my server that uses accounts-password package to add a user:

addUser: function(email, pass, fname, lname, photo, tel, salonId, role, userInfo, order)
{
var userId =
Accounts.createUser(
{
email: email,
password: pass,
profile:
{
firstname: fname,
lastname: lname,
photo: photo,
tel: tel,
sa_info_on_user:
[{
salon_id: salonId,
role: role,
user_info: userInfo,
order: order
}]
}
});

return userId;
}

This creates a user and returns its _id.

Now my jasmine.unit test looks like this:

(function ()
{
"use strict";

describe("User functions: ", function ()
{
it("Add user", function()
{
var spyEvent = spyOn(Meteor.methodMap, 'addUser');
//console.log(spyEvent);

Meteor.methodMap.addUser('a...@asd.asd', 'asdasd', 'John', 'Doe', 'not yet', '070-111 22 33', 'ididididid', 'admin', 'info admin john', 0);
 
expect(spyEvent).toHaveBeenCalled();
expect(spyEvent).toHaveBeenCalledWith('a...@asd.asd', 'asdasd', 'John', 'Doe', 'not yet', '070-111 22 33', 'ididididid', 'admin', 'info admin john', 0);
});
});
})();

And my question is, how can I with this test make sure that the user "John" was created and an id was returned? One reason for this is because I also want to use that id to test removing this created user.

Jonas Aschenbrenner

unread,
Aug 6, 2014, 7:26:36 AM8/6/14
to meteo...@googlegroups.com
Why do you want to test a "third party" package? The package has already tests (https://github.com/meteor/meteor/blob/devel/packages/accounts-password/password_tests.js), so you can assume that it works correctly.
Meteor.methodMap.addUser('asd@asd.asd', 'asdasd', 'John', 'Doe', 'not yet', '070-111 22 33', 'ididididid', 'admin', 'info admin john', 0);
 
expect(spyEvent).toHaveBeenCalled();
expect(spyEvent).toHaveBeenCalledWith('asd@asd.asd', 'asdasd', 'John', 'Doe', 'not yet', '070-111 22 33', 'ididididid', 'admin', 'info admin john', 0);
});
});
})();

Ustag

unread,
Aug 6, 2014, 7:45:44 AM8/6/14
to meteo...@googlegroups.com
Because I would like to test similar functions which are not using any 3rd party packages. I guess this code is a bad example.

Jonas Aschenbrenner

unread,
Aug 6, 2014, 8:10:30 AM8/6/14
to meteo...@googlegroups.com
Ok. I will assume that the code under test is in your code base and similar to your posted example.
When you use jasmine-unit (with velocity), the Meteor collection is automatically mocked. That is necessary to isolate the code under test.
I think you can say that the user was created successfully when Meteor.users.insert was called with your user data.

describe 'Accounts.createUser', ->
  it 'should insert the user into the users collection', ->
    spyOn(Meteor.users, 'insert')
    var userData = { /* User data */ }
    Accounts.createUser(userData)
    expect(Meteor.users.insert).toHaveBeenCalledWith({ /* User data (I don't know the format) */})

Ustag

unread,
Aug 6, 2014, 8:45:02 AM8/6/14
to meteo...@googlegroups.com
I am supposed to do something like this then (using another example) ?

it("Add message", function()
{
var spyEvent = spyOn(Message, 'upsert');

Message.addMessage('bookingId', 'customerId', 'employeeId', 'messageSender', 'messageText', 'messageTime');

expect(spyEvent).toHaveBeenCalled();
});

Doing like this I get: upsert() method does not exist

The real addMessage function looks like this:

addMessage: function(bookingId, customerId, employeeId, messageSender, messageText, messageTime)
{
Message.upsert({ me_booking_id: bookingId, me_customer_id: customerId, me_employee_id: employeeId }, { $addToSet: { me_message: { me_sender: messageSender, me_text: messageText, me_time: messageTime } } });
}

Jonas Aschenbrenner

unread,
Aug 6, 2014, 9:08:46 AM8/6/14
to meteo...@googlegroups.com

Upsert is missing in the mock. I will add it later. https://github.com/alanning/meteor-stubs/blob/master/index.js
You can just add an empty function before spying on it as a workaround.

Gruß
Jonas Aschenbrenner

--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/CKH04KVSvWw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages