Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a custom Meteor method with Velocity + Jasmine [closed]

I have a Collection 'workouts' as follows:

Workouts = new Mongo.Collection('workouts');

Meteor.methods({
  workoutInsert: function () {
    var user = Meteor.user();

    check(user._id, String);

    var workout = {
      completed: false,
      createdAt: new Date(),
      userId: user._id
    };

    var workoutId = Workouts.insert(workout);

    return {
      _id: workoutId
    };
  }
});

I am wondering:

1) What would a Velocity + Jasmine test look like for this method? I'm unsure where to start and would really appreciate and example!

2) Is this the best practice to define this method and call it client-side? Or perhaps should I create a Workout class and add call this method from an instance method of that class? Or should I perhaps extend Workouts to be it's own class and add instance methods to that?

like image 797
delisdeli Avatar asked Mar 18 '15 04:03

delisdeli


1 Answers

In Meteor there is several types of testing: Client Integration, Client Unit, Server Integration and Server Unit.

Integration tests mirror your site and will load in your Meteor methods for you(ie. workoutInsert).

If I were testing this, I might have something such as:

//File Location: app/tests/server/integration/workoutsSpec.js
Jasmine.onTest(function () {
    describe('workouts', function () {
       it("should call to Workouts.insert",function(){

       //Make user return truthy _id for testing
       Meteor.user() = function(){return {_id : "1";}}

       //Setup a spy to watch for calls to Workouts.insert
       spyOn("Workouts",insert);

       //Call workoutInsert Meteor Method
       Meteor.call('workoutInsert');

       //Verify if Workouts.insert was called
       expect("Workouts.insert").toHaveBeenCalled();
       });
    });
});

Lastly, MeteorJS gives you a lot of freedom as to how you implement things and there's no clear best way to do things that works for every scenario. Although, I'd advise against placing any code that interacts with your database on your client. Anything located in your client folder is publicly accessible/readable to your users( Do they need to see low level validation details?).

like image 115
Joe Avatar answered Nov 10 '22 13:11

Joe