• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

test.py 오류

19.09.28 10:11 작성 조회수 532

0

test.py 를 하면 

raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)

ValueError: The 'image' attribute has no file associated with it.

위와 같은 에러가 나옵니다.

*test.py 참고

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


def create_category(name='대외활동'description=''):
    category, is_created = Category.objects.get_or_create(
        name=name,
        description=description,
    )

    category.slug = category.name.replace('''-').replace('/''')
    category.save()

    return category


def create_tag(name='some tag'):
    tag, is_created = Tag.objects.get_or_create(
        name=name,
    )
    tag.slug = tag.name.replace('''-').replace('/''')
    tag.save()

    return tag


def create_post(titleorganizationauthorlinkcategory=None):
    blog_post = Post.objects.create(
        category=category,
        title=title,
        organization=organization,
        birthline=timezone.now(),
        deadline=timezone.now(),
        link=link,
        author=author,
        created=timezone.now(),
        updated=timezone.now(),
    )

    return blog_post


class TestModel(TestCase):
    def setUp(self):
        self.client = Client()
        self.author_000 = User.objects.create(
            username='westline'password='nopassword')

    def test_category(self):
        category = create_category()

        post_000 = create_post(
            title='The first post',
            organization='SK',
            link='https://www.naver.com',
            author=self.author_000,
            category=category,
        )

        self.assertEqual(category.post_set.count(), 1)

    def test_tag(self):
        tag_000 = create_tag(name='bad_guy')
        tag_001 = create_tag(name='america')

        post_000 = create_post(
            title='The first post',
            organization='SK',
            link='https://www.naver.com',
            author=self.author_000,
        )

        post_000.tags.add(tag_000)
        post_000.tags.add(tag_001)
        post_000.save()

        post_001 = create_post(
            title='The second post',
            organization='Samsung',
            link='https://www.naver.com',
            author=self.author_000,
        )

        post_001.tags.add(tag_001)
        post_001.save()

        self.assertEqual(post_000.tags.count(), 2)  # post는 여러개의 tag를 가질 수 있다.
        # 하나의 tag는 여러개의 post에 붙을 수 있다
        self.assertEqual(tag_001.post_set.count(), 2)
        self.assertEqual(tag_001.post_set.first(), post_000)
        # 하나의 tag는 자신으 가진 post들을 불러올 수 있다.
        self.assertEqual(tag_001.post_set.last(), post_001)

    def test_post(self):
        category = create_category()
        post_000 = create_post(
            title='The first post',
            organization='SK',
            link='https://www.naver.com',
            author=self.author_000,
            category=category,
        )


class TestView(TestCase):
    def setUp(self):
        self.client = Client()
        self.author_000 = User.objects.create(
            username='westline'password='nopassword')

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

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

        self.assertIn(title.text, '대외활동의 모든 것')

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

    def test_post_list_with_post(self):
        post_000 = create_post(
            title='The first post',
            organization='SK',
            link='https://www.naver.com',
            author=self.author_000,
        )

        post_001 = create_post(
            title='The second post',
            organization='SAMSUNG',
            link='https://www.daum.net',
            author=self.author_000,
        )

        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_000.title, body.text)

답변 2

·

답변을 작성해보세요.

0

bewoody님의 프로필

bewoody

질문자

2019.10.01

감사합니다! 맞습니다 강의보면서 공부하고 적용시켜보고있지만 쉽지않네요ㅠㅠ

0

제 강좌에서 약간 변형한 내용으로 웹사이트를 만들고 계신 것 같습니다. 

에러메세지 내용은 image 필드에 파일이 채워져야 한다는 뜻입니다. 어떤 모델인지 알 수 없으나, 그 모델에 image라는 이름의 필드를 만드신 것 같습니다. image 필드에blank=True, null=True 가 선언되어 있지 않아 무조건 채워져야 하는 것으로 되어 있어서 저런 메세지가 나오는 것이 아닐까 싶네요. 

해결방안은 models.py를 열어서 image 필드에 blank=True, null=True를 추가해주시고, 마이그레이션 하시면 될 것 같습니다.