neo post modern
neo post modern

Reputation: 3424

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

Upvotes: 0

Views: 176

Answers (1)

Xolv.io
Xolv.io

Reputation: 2533

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.

Upvotes: 2

Related Questions