Skip to content Skip to sidebar Skip to footer

Mocking Render To Response With Pyramid

I have a decorator that looks like so: def validate_something(func): def validate_s(request): if request.property: render_to_response('template.jinja', 'err

Solution 1:

Mock library is awesome:

mock provides a core Mock class removing the need to create a host of stubs throughout your test suite. After performing an action, you can make assertions about which methods / attributes were used and arguments they were called with. You can also specify return values and set needed attributes in the normal way.

Additionally, mock provides a patch() decorator that handles patching module and class level attributes within the scope of a test

Youc code would look like this:

deftest_something(self):
    test_func = Mock.MagicMock(return_value=1)  # replaced stub function with a mock
    request = testing.DummyRequest()
    # patching a Pyramid method with a mockwith mock.patch('pyramid.renderers.render_to_response'as r2r:
        resp = validate_something(test_func(request))
        result = resp(request)
        assert r2r.assert_called_with('template.jinja', 'error')

Solution 2:

The following worked for me:

defsetUp(self):
    self.app = TestApp(target_app())
    self.config = testing.setUp(request=testing.DummyRequest) 
    self.config.include('pyramid_jinja2')

By setting up the config to include jinja your tests can then find your template and the jinja environment. You may also need to provide a test version of the template in the same folder as your tests. If you get a message such as TemplateNotFound on running the tests then make sure a version of the template is located in the correct place.

Post a Comment for "Mocking Render To Response With Pyramid"