MeteorJS: how to silence a proven method in unit test

I am using proven methods ( mdg:validated-method ) with LoggedInMixin ( tunifight:loggedin-mixin ).

Now I have a problem with my unit tests, because they fail with the notLogged error, because of course, the user is not registered in unit tests. How do I need to drown it out?

method

 const resetEdit = new ValidatedMethod({ name: 'reset', mixins: [LoggedInMixin], checkLoggedInError: { error: 'notLogged' }, // <- throws error if user is not logged in validate: null, run ({ id }) { // ... } }) 

unit test

 describe('resetEdit', () => { it('should reset data', (done) => { resetEdit.call({ id: 'IDString' }) }) }) 

Unit tests throw Error: [notLogged] .

+7
javascript unit-testing meteor stub
source share
1 answer

Edit:

validated-method has a built-in way of providing context and is documented in README , exactly for cases like the one asked in your question.

method # _execute (context: object, args: object)

Call this from your test code to simulate a method call on behalf of a specific user:

( source )

  it('should reset data', (done) => { resetEdit._execute({userId: '123'}, { id: 'IDString' }); done(); }); 

Original answer:

I believe this can be achieved using the DDP._CurrentMethodInvocation meteor shower environment variable.

If you run the test in an area where its value is the string a userId , it will be merged with the rest of the context object of the method call, and mixin will not work.

 describe('resetEdit', () => { it('should reset data', (done) => { DDP._CurrentMethodInvocation.withValue({userId: '123'}, function() { console.log(DDP._CurrentInvocation.get()); // {userId: '123'} resetEdit.call({ id: 'IDString' }); done(); }) }); }) 
+1
source share

All Articles