Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Basics Test Time Test our Writer model

Not sure what the question is asking me. After checking work my test didn't pass.

Question says I have to get the output of the email attribute. Does that mean that the mailto function should return an email? If so, I changed the code around and made it return only an email by doing: return '<{}>'.format(self.email) in the other py file.

articles/tests.py
from django.test import TestCase
from .models import Writer


class WriterModelTestCase(TestCase):
    '''Tests for the Writer model'''

    def test_writer_creation(self):
        writer = Writer.objects.create()
        self.assertIn(writer.mailto())
articles/models.py
from django.db import models


class Article(models.Model):
    headline = models.CharField(max_length=255)
    publish_date = models.DateTimeField()
    content = models.TextField()
    writer = models.ForeignKey('Writer')

    def __str__(self):
        return self.headline


class Writer(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField()
    bio = models.TextField()

    def __str__(self):
        return self.name

    def mailto(self):
        return '{} <{}>'.format(self.name, self.email)

1 Answer

Brandon Oakes
seal-mask
.a{fill-rule:evenodd;}techdegree
Brandon Oakes
Python Web Development Techdegree Student 11,501 Points

The question wants you to test to see that an instance created from the Writer class has its email attribute in the instance.mailto().

self.assertIn test to see if a is in b, self.assertIn(a,b). So make an instance and than test to see if the email attribute is in the mailto() method. The code below worked for me

from django.test import TestCase from .models import Writer

class WriterModelTestCase(TestCase): '''Tests for the Writer model'''

def test_email(self):
    instance = Writer()
    self.assertIn(instance.email, instance.mailto())

Thank you!