The mailer is a utility that represents an external service. It is not
different per-request just like how your smtp server is not different
per email you send.

Using .clear() is appropriate here.

On Mon, Dec 22, 2014 at 2:04 PM, pyramidX <[email protected]> wrote:
> In a given unit test function I create 2 requests, each send one email. Even
> though I'm passing 2 different instances of DummyRequest to get_mailer, the
> DummyMailer objects returned are the same instance. You can see in my last
> assert statement it fails because there are 2 emails in the outbox (one
> created by request1 and the other by request2).
>
> I can do a mailer1.outbox.clear() to get the expected behavior. But is this
> the right way of doing this? How come get_mailer behaves like this,
> returning the same instance every time?
>
> request1 = testing.DummyRequest(...)
> views.send_one_email(...)
> mailer1 = get_mailer(request1)
>
> # mailer1.outbox.clear()  # Why is this necessary for the last assert below
> not to fail?
>
> request2 = testing.DummyRequest(...)
> views.send_one_different_email(...)
> mailer2 = get_mailer(request2)
>
> assert request1 is not request2  # Not the same request instances
> assert mailer1 is mailer2  # It's the same mailer instance
> assert len(mailer1.outbox) == 1
> assert len(mailer2.outbox) == 1  # Fails
>
> It works the same if I do it like this, which wouldn't make me think there
> will be 2 different mailers returned, but in this case what's the purpose of
> passing a DummyRequest to get_mailer?
>
> single_mailer = get_mailer(testing.DummyRequest())
>
> request1 = testing.DummyRequest(...)
> views.send_one_email(...)
>
> request2 = testing.DummyRequest(...)
> views.send_one_different_email(...)
>
> assert request1 is not request2  # Not the same request instances
> assert len(single_mailer.outbox) == 1
> assert len(single_mailer.outbox) == 1  # Fails
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to