cannot get mailer in test case using tgext.mailer

22 views
Skip to first unread message

NIWDEK

unread,
Apr 14, 2015, 12:37:13 AM4/14/15
to turbo...@googlegroups.com
Hi All,

I am using tgext.mailer to send email and I found that it is testable for email testing by setting debugMailer to dummy

Then, in the functional test case, I try to get mailer to assertion "mailer = get_mailer(tg.request)"

However, I got an exception

Traceback (most recent call last):
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.3/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Users/xxx/Workspace/yyy/yyy/yyy/tests/functional/test_subscriber.py", line 28, in test_subscribe_with_email_should_send_email
    mailer = get_mailer(tg.request)
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.3/site-packages/tgext/mailer/__init__.py", line 22, in get_mailer
    return request._mailer
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.3/site-packages/tg/support/objectproxy.py", line 19, in __getattr__
    return getattr(self._current_obj(), attr)
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.3/site-packages/tg/request_local.py", line 171, in _current_obj
    return getattr(context, self.name)
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.3/site-packages/tg/support/objectproxy.py", line 19, in __getattr__
    return getattr(self._current_obj(), attr)
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.3/site-packages/tg/support/registry.py", line 69, in _current_obj
    'thread' % self.____name__)
nose.proxy.TypeError: No object (name: context) has been registered for this thread

I wonder how should I get a mailer in test case. Thanks

NIWDEK

unread,
Apr 14, 2015, 12:12:44 PM4/14/15
to turbo...@googlegroups.com
I have tried to use get_mailer(None) to obtain the global one, but got "{AttributeError}'Globals' object has no attribute '_mailer' "

Then I trace the tg.app_globals object, I found it is initialized twice , one is in load_app() and another one is in setup_app(). Is that behavior normal?

Alessandro Molina

unread,
Apr 16, 2015, 6:32:47 PM4/16/15
to TurboGears
As you test is not performing any request, you cannot get the mailer from tg.request (there is no tg.request being served).
You should retrieve the previous request (the one that sent the email) from the testing variables and then get the mail of that request.

Like:

resp = self.app.get('/send_email')

ctx = resp.req.environ['paste.testing_variables']

mailer = get_mailer(ctx['req']) 

sent_email = mailer.outbox[0]


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

Reply all
Reply to author
Forward
0 new messages