title: Stuck with Test-Driven Development with Python (chapter 6)
body: <p>I learn Test-Driven Development with Python by Harry Percival.</p>
  
  <p>I stuck in the beginning of chapter  6 approximately here: </p>
  
  <p><a href="http://chimera.labs.oreilly.com/books/1234000000754/ch06.html#_running_just_the_unit_tests" rel="nofollow">http://chimera.labs.oreilly.com/books/1234000000754/ch06.html#_running_just_the_unit_tests</a></p>
  
  <p>To be more precise at the point of "python3 manage.py test lists".</p>
  
  <p>Let's quote the problematic code:</p>
  
  <pre><code>lists/tests.py
  class HomePageTest(TestCase):
  
      def test_home_page_returns_correct_html(self):
          request = HttpRequest()
          response = home_page(request)
          expected_html = render_to_string('home.html')
          self.assertEqual(response.content.decode(), expected_html)
  
  lists/views.py
  def home_page(request):
      if request.method == 'POST':
          Item.objects.create(text=request.POST['item_text'])
          return redirect('/')
  
      items = Item.objects.all()
      return render(request, 'home.html', {'items': items})
  
  lists/templates/home.html
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;To-Do lists&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Your To-Do list&lt;/h1&gt;
          &lt;form method="POST"&gt;
              &lt;input name="item_text" id="id_new_item" placeholder="Enter a to-do item" /&gt;
              {% csrf_token %}
          &lt;/form&gt;
  
          &lt;table id="id_list_table"&gt;
              {% for item in items %}
                  &lt;tr&gt;&lt;td&gt;{{ forloop.counter }}: {{ item.text }}&lt;/td&gt;&lt;/tr&gt;
              {% endfor %}
          &lt;/table&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The whole code from the previous chapter (number 5) is here: <a href="https://github.com/hjwp/book-example/tree/chapter_05" rel="nofollow">https://github.com/hjwp/book-example/tree/chapter_05</a></p>
  
  <p>The tests in lists application were not changed since chapter 5. 
  At the point where I stuck, all tests from <strong>lists</strong> application pass (according to the book).</p>
  
  <p>But I get this:</p>
  
  <pre><code>======================================================================
  FAIL: test_home_page_returns_correct_html (lists.tests.HomePageTest)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/home/michael/workspace/superlists/lists/tests.py", line 20, in test_home_page_returns_correct_html
      self.assertEqual(response.content.decode(), expected_html)
  AssertionError: '&lt;htm[238 chars]     &lt;input type=\'hidden\' name=\'csrfmiddlew[163 chars]l&gt;\n' != '&lt;htm[238 char
  s]     \n        &lt;/form&gt;\n\n        &lt;table id="i[66 chars]l&gt;\n'
  
  ----------------------------------------------------------------------
  </code></pre>
  
  <p>I try to evaluate expression in the debugger and get this:</p>
  
  <pre><code>"expected_html" SafeText:        &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;To-Do lists&lt;/title&gt;\n    &lt;/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Your To-Do list&lt;/h1&gt;\n        &lt;form method="POST"&gt;\n            &lt;input name="item_text" id="id_new_item" placeholder="Enter a to-do item" /&gt;\n            \n        &lt;/form&gt;\n\n        &lt;table id="id_list_table"&gt;\n            \n        &lt;/table&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n 
  
  "response.content.decode()" str: &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;To-Do lists&lt;/title&gt;\n    &lt;/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Your To-Do list&lt;/h1&gt;\n        &lt;form method="POST"&gt;\n            &lt;input name="item_text" id="id_new_item" placeholder="Enter a to-do item" /&gt;\n            \n        &lt;/form&gt;\n\n        &lt;table id="id_list_table"&gt;\n            \n        &lt;/table&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n
  </code></pre>
  
  <p>Well, first one is SafeText and the second one is str.</p>
  
  <p>If I convert expected_html to string (expected_html = str(render_to_string('home.html')), the test still fails:</p>
  
  <pre><code>======================================================================
  FAIL: test_home_page_returns_correct_html (lists.tests.HomePageTest)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/home/michael/workspace/superlists/lists/tests.py", line 20, in test_home_page_returns_correct_html
      self.assertEqual(response.content.decode(), expected_html)
  AssertionError: '&lt;htm[238 chars]     &lt;input type=\'hidden\' name=\'csrfmiddlew[163 chars]l&gt;\n' != '&lt;htm[238 char
  s]     \n        &lt;/form&gt;\n\n        &lt;table id="i[66 chars]l&gt;\n'
    &lt;html&gt;
        &lt;head&gt;
            &lt;title&gt;To-Do lists&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
            &lt;h1&gt;Your To-Do list&lt;/h1&gt;
            &lt;form method="POST"&gt;
                &lt;input name="item_text" id="id_new_item" placeholder="Enter a to-do item" /&gt;
  -             &lt;input type='hidden' name='csrfmiddlewaretoken' value='08xKa5oXRi6EEsEBmAqONg8ATnKeqXOE' /&gt;
  +             
            &lt;/form&gt;
  
            &lt;table id="id_list_table"&gt;
  
            &lt;/table&gt;
        &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>
  
  <p>Could you give me a kick here to pass the test?</p>