How to test custom Meteor method with Velocity + Jasmine

I have a β€œworkout” collection 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 will be the Velocity + Jasmine test for this method? I do not know where to start, and I will be very grateful and an example!

2) Is it best practice to define this method and call it the client side? Or maybe I should create a workout class and add a call to this method from an instance method of this class? Or should I possibly extend Workouts to be its own class, and add instance methods to it?

+6
source share
2 answers

There are several types of testing in Meteor: client integration, client block, server integration, and server block.

Integration tests reflect your site and will be loaded into your Meteor methods for you (i.e. workoutInsert).

If I tested this, I could have something like:

 //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(); }); }); }); 

Finally, MeteorJS gives you a lot of freedom about how you implement things, and there is no clear better way to do what works for each scenario. Although, I would advise against posting code that interacts with your database on your client. Everything in the client’s folder is accessible for users / readable (Do I need to see the verification details at a low level?).

+3
source

To answer your second question, it is best to use the Meteor methods in the server directory. Meteor uses these reserved directory names to give you control over resources that are served by a client, server, or both. You do not have to have them in the same file or directory as your Mongo collections, since all your collections can be accessed both on the client and on the server. This is usually considered best practice, especially if you use frameworks such as angular-meteor, which rely on Collection definitions available on the client so that filters can be passed to them. You can protect and change permissions for these Collections by using collection.allow()/deny()

So, if you saved all your collections in the collections/ directory, they can be defined as follows:

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

will be the contents of collections/workouts.js

Then, in your server/ directory at the same level as your collections/ , you can put all your methods in a file at that level or deeper in the tree, for example, in the server/methods/ directory. Then you can put your methods in workouts.js in this directory if you want.

 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 }; } }); 
0
source

Source: https://habr.com/ru/post/983945/


All Articles