-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit Test Examples #54
Comments
I am using this kind of pattern to test a method that throws an error chai.assert.throws(() => {
insertDocument._execute({ /* user id if required*/}, {/*method params*/});
}, Meteor.Error, /you error message/
); hope it can help you. |
There are a ton of examples here: https://github.com/meteor/todos/blob/master/imports/api/lists/lists.tests.js Do those cover your case? |
Thanks for the quick reply. Just to clarify, you are using Also, when I try out the example above (using
Also, when I change the last parameter of
instead of:
The server test passes, but the client test still fails with the same error. |
I can't debug with that little information - can you paste more code? But the reason we use
|
I apologize. Here is a more detailed code sample. I am still new to Meteor and this kind of JavaScript programming in general, so I really appreciate your help and your patience: // the test method
it('fails to insert a shift alert because start date is in the past', function () {
var name = 'Invalid Shift - Happened in the Past';
var date = moment().subtract(3, 'days');
assert.throws(() => {
insertShiftAlert.call({
groupId: faker.random.uuid(),
name: name,
start: date.toDate(),
end: date.add(12, 'hours').toDate(),
slots_available: 1,
sub_shifts_available: false
})
}, Meteor.Error, /Error: Start Date cannot be in the past/);
});
// the method I am trying to test
export const insertShiftAlert = new ValidatedMethod({
name: 'shift-alerts.insert',
validate: new SimpleSchema({
groupId: { type: String },
name: { type: String },
start: { type: Date },
end: { type: Date },
slots_available: { type: Number },
sub_shifts_available: { type: Boolean }
}).validator(),
run(shiftAlert) {
ShiftAlerts.insert(shiftAlert);
},
});
// the portion of the SimpleSchema definition that does validation on start date
...
start: {
type: Date,
label: 'The start date/time of the shift',
custom: function () {
if (moment(this.value).diff(moment()) < 0 ) {
return "startDateInPast";
}
},
srf: {
type: 'text',
},
},
... |
@stubailo Is it possible to create a test to check if a given method its using a mixin? In my case, I have a method that updates a document just if the user is the owner of the document, so I created I mixin to check this using the Please check our test implementation here. |
Could you provide some unit test examples or a pattern for using a framework like chai? I am using the Meteor Chef's Base template, and I am trying to extend this file to include a test for when the insertDocument.call() function fails validation and throws an Error.
I have tried a ton of different things, but I can't seem to get it to work. Could you provide some sample code for that?
The text was updated successfully, but these errors were encountered: