python - Testing page content after redirect not working -


i writing simple unit test method tests view redirecting after receiving post. can test expected url matches response object not content.

current shoddy code:

def test_example_view_redirects_to_home_after_post(self):     client = client()     response = client.post('/example/')      # test location     self.assertequal(response.get('location'), "http://testserver/")      # test page content     expected_template = render_to_string('home.html')     self.assertequal(response.content, expected_template) 

the test failing:

  assertionerror: b'' != '<html> etc etc... 

the view code can ultra simple @ stage:

def example(request):     if request.method == "post":         return httpresponseredirect('/')     else:         return render(request, 'example.html') 

i new tdd , wondering why isn't working might expect to.

thanks.

  1. you can use follow=true in client.post emulate browser following redirect next page. see here in docs. allow test second page's content assertcontains.

  2. you can use assertredirects test redirect happens properly. check out here in docs.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -