Pat*_*son 2 django django-unittest factory-boy
我需要一个允许员工用户查看草稿状态对象的视图.但我发现很难为这个观点编写单元测试.
我正在使用Factory Boy进行设置:
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
username = factory.LazyAttribute(lambda t: random_string())
password = factory.PostGenerationMethodCall('set_password', 'mysecret')
email = fuzzy.FuzzyText(
length=12, suffix='@email.com').fuzz().lower()
is_staff = True
is_active = True
class ReleaseFactory(factory.django.DjangoModelFactory):
class Meta:
model = Release
headline = factory.LazyAttribute(lambda t: random_string())
slug = factory.LazyAttribute(lambda t: slugify(t.headline))
author = factory.LazyAttribute(lambda t: random_string())
excerpt = factory.LazyAttribute(lambda t: random_string())
body = factory.LazyAttribute(lambda t: random_string())
class TestReleaseViews(TestCase):
"""
Ensure our view returns a list of :model:`news.Release` objects.
"""
def setUp(self):
self.client = Client()
self.user = UserFactory.create()
self.client.login(username=self.user.username, password=self.user.password)
Run Code Online (Sandbox Code Playgroud)
鉴于我现在有一个已登录的员工用户进行测试,我该如何使用它来测试视图(status_code 200而不是404)?
例如,当我的视图允许具有is_staffTrue的用户访问视图时,此测试失败(404!= 200):
def test_staff_can_view_draft_releases(self):
"ReleaseDetail view should return correct status code"
release = ReleaseFactory.create(status='draft')
response = self.client.get(
reverse(
'news:release_detail',
kwargs={
'year': release.created.strftime('%Y'),
'month': release.created.strftime('%b').lower(),
'day': release.created.strftime('%d'),
'slug': release.slug
}
)
)
self.assertEqual(response.status_code, 200)
Run Code Online (Sandbox Code Playgroud)
小智 6
实际上,404由于self.client.login调用失败,您收到错误.
当你通过时password=self.user.password,你发送密码的哈希值,而不是密码本身.
当你打电话时UserFactory(),factory_boy在你工厂采取的步骤是:
{'username': "<random>", 'is_active': True, 'is_staff': True, 'email': "<fuzzed>@email.com"}save() 它user.set_password('my_secret')user.save()再打电话届时,user.password是结果的set_password('my_secret'),不是'my_secret'.
我会去(在你的测试中):
pwd = 'my_super_secret'
self.user = UserFactory(password=pwd)
self.client = Client()
self.assertTrue(self.client.login(username=self.user.username, password=pwd))
Run Code Online (Sandbox Code Playgroud)
顺便说一下,你的电子邮件字段的声明将不会像你期望的那样工作:当你写的时factory.fuzzy.FuzzyText(...).fuzz().lower(),这只会在UserFactory声明类时执行一次.
你应该改用factory.fuzzy.FuzzyText(chars='abcdefghijklmnopqrstuvwxyz', length=12, suffix='@example.com').
| 归档时间: |
|
| 查看次数: |
908 次 |
| 最近记录: |