On Monday, November 21, 2016 at 9:22:38 AM UTC-8, Jeffrey Ratcliffe wrote:
>
> 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 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/trac-dev.
For more options, visit https://groups.google.com/d/optout.

Reply via email to