Skip to content
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

Open
dvmorris opened this issue May 24, 2016 · 6 comments
Open

Unit Test Examples #54

dvmorris opened this issue May 24, 2016 · 6 comments

Comments

@dvmorris
Copy link

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?

@dvmorris dvmorris changed the title Mocha Test Examples Unit Test Examples May 24, 2016
@ghost
Copy link

ghost commented May 24, 2016

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.

@stubailo
Copy link
Contributor

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?

@dvmorris
Copy link
Author

dvmorris commented May 24, 2016

Thanks for the quick reply. Just to clarify, you are using ._execute instead of ._call to run the function. Does that make a difference?

Also, when I try out the example above (using .call instead of .execute), I am getting the following errors when running the tests with mocha:

  • Server: Error: expected [Function] to throw 'Error' but 'Error: Start Date cannot be in the past' was thrown
  • Client: AssertionError: expected [Function] to throw Error

Also, when I change the last parameter of assert.throws() to:

}, /Start Date cannot be in the past/

instead of:

}, Meteor.Error, /Error: Start Date cannot be in the past/);

The server test passes, but the client test still fails with the same error.

@stubailo
Copy link
Contributor

I can't debug with that little information - can you paste more code? But the reason we use _execute is so that we can:

  1. Avoid making a network request in a unit test
  2. Pass in custom stuff for this, like userId, etc

@dvmorris
Copy link
Author

dvmorris commented May 24, 2016

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',
    },
  },
...

@lnmunhoz
Copy link

lnmunhoz commented Jun 2, 2016

@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 ziarno:restrict-mixin. Do you have any example about a possible implementation using chai?

Please check our test implementation here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants