I am using validated methods (mdg:validated-method
) with LoggedInMixin (tunifight:loggedin-mixin
).
Now I have a problem with my unit tests, as they fail with notLogged
error, because in the unit tests there is no logged user of course.
How do I have to stub that?
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 throws Error: [notLogged]
.
validated-method
has a built-in way of providing a context and it is documented in the README
, exactly for cases like the one in your question.
method#_execute(context: Object, args: Object)
Call this from your test code to simulate calling a method on behalf of a particular user:
(source)
it('should reset data', (done) => {
resetEdit._execute({userId: '123'}, { id: 'IDString' });
done();
});
I believe that this can be achieved using the DDP._CurrentMethodInvocation
meteor environment variable.
If you run the test in a scope where the its value is an object a userId
string, it will be merged with the rest of the method invocation context object and the mixin will not fail.
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();
})
});
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With