• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

tdd 테스트코드 작성 B에서 아래와 같은 에러가 납니다.

20.08.10 08:41 작성 조회수 251

0

test 누르면 아직 게시물이 없습니다. 에서 자꾸 에러가 나요.

(venv) C:\Users\user\Desktop\HTML+django\my_django>python manage.py test

Creating test database for alias 'default'...

System check identified no issues (0 silenced).

F

======================================================================

FAIL: test_post_list (blog.tests.TestView)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "C:\Users\user\Desktop\HTML+django\my_django\blog\tests.py", line 30, in test_post_list

    self.assertIn('아직 게시물이 없습니다.', soup.body.text)

AssertionError: '아직 게시물이 없습니다.' not found in '\n\n\nBootswatch\n\n\n\n\n\n\nThemes \n\nDefault\n\nCerulean\nCosmo\nCyborg\nDarkly\nFlatly\nJournal\nLitera\nLumen\nLux\nMateria\nMinty\nPulse\nSandstone\nSimplex\nSketchy\nSlate\nSolar\nSpacelab\nSuperhero\nUnited\nYeti\n\n\n\nHelp\n\n\nBlog\n\n\nFlatly \n\nOpen in JSFiddle\n\nbootstrap.min.css\nbootstrap.css\n\n_variables.scss\n_bootswatch.scss\n\n\n\n\n\n\n\n\n\n\nBlog\nby matiii\n'

----------------------------------------------------------------------

Ran 1 test in 0.037s

FAILED (failures=1)

Destroying test database for alias 'default'...

post_list.html

<div class="col-md-8">
<h1 class="my-4">Blog</h1>
<small>by matiii</small>
</h1>
<!-- Blog Post -->
{% if objects_list.exist %}
{% for p in object_list %}
<div class="card mb-4">
{% if p.head_image %}
<img class="card-img-top" src="{{ p.head_image.url }}" alt="Card image cap">
{% else %}
<img class="card-img-top" src="https://picsum.photos/750/300/?random" alt="Card image cap">
{% endif %}
<div class="card-body">
<h2 class="card-title">{{ p.title }}</h2>
<p class="card-text">{{ p.content | truncatewords:50 }}</p>
<a href="#" class="btn btn-primary">Read More &rarr;</a>
</div>
<div class="card-footer text-muted">
Posted on {{p.created}}
<a href="#">{{p.author}}</a>
</div>
</div>


<!-- Pagination -->
<ul class="pagination justify-content-center mb-4">
<li class="page-item">
<a class="page-link" href="#">&larr; Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#">Newer &rarr;</a>
</li>
</ul>

{% endfor %}
{% else %}
<h3>아직 게시물이 없습니다.</h3>
{% endif %}

</div>

tests.py

from django.test import TestCase

# Create your tests here.

from django.test import TestCase, Client
from bs4 import BeautifulSoup
from .models import Post
from django.utils import timezone
from django.contrib.auth.models import User

class TestView(TestCase):
def setUp(self):
self.client = Client()
self.author_OOO=User.objects.create(username='SAM', password='none')

def test_post_list(self):
response = self.client.get('/blog/')
self.assertEqual(response.status_code, 200)

soup = BeautifulSoup(response.content, 'html.parser')
title = soup.title

self.assertEqual(title.text, 'Blog')

navbar = soup.find('div', id='navbar')
self.assertIn('Blog', navbar.text)
self.assertIn('Help', navbar.text)

self.assertEqual(Post.objects.count(), 0)
self.assertIn('아직 게시물이 없습니다.', soup.body.text)

post_OOO = Post.objects.create(
title='The First Post',
content='Hello World!',
created=timezone.now(),
author=self.author_OOO,
)

self.assertGreater(Post.objects.count(), 0)

response = self.client.get('/blog/')
self.assertEqual(response.status_code, 200)

soup = BeautifulSoup(response.content, 'html.parser')
body = soup.body
self.assertNotIn('아직 게시물이 없습니다.', body.text)
self.assertIn(post_OOO.title, body.text)

답변 1

답변을 작성해보세요.

0

{% if objects_list.exist %}

에서 exist가 아니라 exists 아닐까요??