Search code examples
testingmeteormeteor-velocity

Test if email is send in Meteor Velocity


Is it possible to confirm emails are being sent in Meteor Velocity tests?

I thought I could just have a method in tests with the same name that override/duplicates the method, but that doesn't work. I tried this:

In my regular code:

Meteor.methods(
   email: (parameters) ->
      sendAnEmail(parameters)
)

In tests:

Meteor.methods(
   email: (parameters) ->
      differentBehaviorForTesting(parameters)
      # I could call some super() here if necessary
)

But this always gets me a

Error: A method named 'email' is already defined

Solution

  • You can also create an email fixture that looks something like this:

      var _fakeInboxCollection = new Package['mongo'].Mongo.Collection('Emails');
    
      Meteor.startup(function () {
        _clearState();
        _initFakeInbox();
      });
    
      Meteor.methods({
        'clearState': _clearState,
        'getEmailsFromInboxStub': function () {
          return _fakeInboxCollection.find().fetch()
        }
      });
    
      function _initFakeInbox () {
        _fakeInboxCollection.remove({});
        Email.send = function (options) {
          _fakeInboxCollection.insert(options);
        };
      }
    
      function _clearState () {
        _fakeInboxCollection.remove({});
      }
    

    This will allow you to send emails normally and also clear / fetch the emails using DDP.