Discussion:
[Trac-dev] trac + mock
Jeffrey Ratcliffe
2016-11-21 17:22:36 UTC
Permalink
I'm trying to use mock to test parts of my trac plug-in.

If I try something like:

def test_process_request(self):
""" Test the process_request method """

with patch('trac.web.api.Request', return_value=None) as mock_req:
mock_req.method = 'POST'
mock_req.args.base_path = 'mybasepath:port'
mock_req.args.project = 'proj'
mock_req.args.milestone = 'ms1'

with patch('trac.web.api.Request.redirect') as mock_redirect:
mock_redirect.return_value = '?project=proj&milestone=ms1'

self.assertEqual(MyPluginClass.process_request(ComponentManager(),
trac.web.api.Request(EnvironmentStub(), None)),
'mybasepath:port?project=proj&milestone=ms1',
'POST triggers redirect')

I get the error message:
TypeError: unbound method process_request() must be called with
MyPluginClass instance as first argument (got ComponentManager
instance instead)

If I change the assertion to:


self.assertEqual(MyPluginClass.process_request(MyPluginClass(),
trac.web.api.Request(EnvironmentStub(), None)),
'mybasepath:port?project=proj&milestone=ms1',
'POST triggers redirect')

I get:
AssertionError: First argument must be a ComponentManager instance

How can I do this?

Regards

Jeff
--
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-dev+***@googlegroups.com.
To post to this group, send email to trac-***@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-dev.
For more options, visit https://groups.google.com/d/optout.
RjOllos
2016-11-21 17:30:51 UTC
Permalink
Post by Jeffrey Ratcliffe
I'm trying to use mock to test parts of my trac plug-in.
""" Test the process_request method """
mock_req.method = 'POST'
mock_req.args.base_path = 'mybasepath:port'
mock_req.args.project = 'proj'
mock_req.args.milestone = 'ms1'
mock_redirect.return_value = '?project=proj&milestone=ms1'
self.assertEqual(MyPluginClass.process_request(ComponentManager(),
trac.web.api.Request(EnvironmentStub(), None)),
'mybasepath:port?project=proj&milestone=ms1',
'POST triggers redirect')
TypeError: unbound method process_request() must be called with
MyPluginClass instance as first argument (got ComponentManager
instance instead)
self.assertEqual(MyPluginClass.process_request(MyPluginClass(),
trac.web.api.Request(EnvironmentStub(), None)),
'mybasepath:port?project=proj&milestone=ms1',
'POST triggers redirect')
AssertionError: First argument must be a ComponentManager instance
How can I do this?
Regards
Jeff
You haven't instantiated an instance of MyPluginClass. Try:

env = EnvironmentStub()
my_plugin = MyPluginClass(env)

Also, you might want to use MockRequest (since 1.0.11):
https://trac.edgewall.org/browser/tags/trac-1.0.13/trac/test.py#L127

- Ryan
--
You received this message because you are subscribed to the Google Groups "Trac Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-dev+***@googlegroups.com.
To post to this group, send email to trac-***@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-dev.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...