运行manage.py test
时收到以下错误:
======================================================================
ERROR: test_pokemon_detail_view (pokedex.tests.test_views.TestViews)
Test the pokemon detail view and ensure the correct template was used
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/z/chbresser/pokedex/tests/test_views.py", line 32, in test_pokemon_detail_view
response = self.client.get(reverse("pokedex:pokemon", args=[self.pokemon.id]))
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 517, in get
response = super().get(path, data=data, secure=secure, **extra)
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 332, in get
return self.generic('GET', path, secure=secure, **r)
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 404, in generic
return self.request(**r)
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 485, in request
raise exc_value
File "/home/z/test_env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/z/test_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/z/test_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/z/test_env/lib/python3.5/site-packages/django/views/decorators/http.py", line 40, in inner
return func(request, *args, **kwargs)
File "/home/z/chbresser/pokedex/views.py", line 23, in pokemon
poke = Pokemon(poke_id)
File "/home/z/chbresser/pokedex/pokemon.py", line 152, in __init__
gender_obj = PokemonGenderRatios.objects.select_related('ratio').get(pokemon=self.id)
File "/home/z/test_env/lib/python3.5/site-packages/django/db/models/query.py", line 403, in get
self.model._meta.object_name
pokedex.models.DoesNotExist: PokemonGenderRatios matching query does not exist.
----------------------------------------------------------------------
这通常意味着数据库中没有PokemonGenderRatios对象,但是我在setUp函数中创建了它:
def setUp(self):
""" Set Up for testing """
self.pokemon = PokemonFactory(species__evolves_from_species=None)
PokemonGenderRatioFactory(
pokemon__species__evolves_from_species__evolves_from_species=None)
def test_pokemon_detail_view(self):
""" Test the pokemon detail view and ensure the correct template was used """
response = self.client.get(reverse("pokedex:pokemon", args=[self.pokemon.id]))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'pokedex/pokemon.html')
有人知道为什么会出错吗?我尝试使用--verbosity 3
进行测试,但是没有给出测试的更多详细信息。
views.py
:
@require_safe
def pokemon(request, poke_id):
""" View for /pokedex/pokemon/<poke_id> """
if int(poke_id) > 721:
return HttpResponseNotFound('''<h1 style="text-align: center">Sorry, you have went to an \
invalid page. :|</h1>'''
'''<h1 style="text-align: center">If this was in error,
contact admin@chbresser.com</h1>''')
poke = Pokemon(poke_id)
context = {'pokemon': poke}
return render(request, 'pokedex/pokemon.html', context)
factories.py
:
class PokemonGenderRatioFactory(factory.django.DjangoModelFactory):
""" Factory for Pokemon Gender Ratios Model """
class Meta:
model = PokemonGenderRatios
pokemon = factory.SubFactory(PokemonFactory)
ratio = factory.SubFactory(_GenderRatioFactory)
class _GenderRatioFactory(factory.django.DjangoModelFactory):
""" Private Factory For GenderRatios Model """
class Meta:
model = GenderRatios
percent_male = '50'
percent_female = '50'
class PokemonFactory(factory.django.DjangoModelFactory):
""" Factory for Pokemon Model """
class Meta:
model = Pokemon
identifier = 'Pokemon Identity'
species = factory.SubFactory(_PokemonSpeciesFactory)
height = 10
weight = 10
base_experience = 10
order = 1
is_default = False
class _PokemonSpeciesFactory(factory.django.DjangoModelFactory):
""" Private Factory for PokemonSpecies Model """
class Meta:
model = PokemonSpecies
identifier = 'Species Identity'
generation = factory.SubFactory(_GenerationFactory)
evolves_from_species = factory.SubFactory('pokedex.tests.factories._PokemonSpeciesFactory')
evolution_chain = factory.SubFactory(_EvolutionChainFactory)
color = factory.SubFactory(_PokemonColorFactory)
shape = factory.SubFactory(_PokemonShapeFactory)
habitat = factory.SubFactory(_PokemonHabitatFactory)
gender_rate = 50
capture_rate = 40
base_happiness = 20
is_baby = False
hatch_counter = 2
has_gender_differences = False
growth_rate = factory.SubFactory(_GrowthRateFactory)
forms_switchable = False
order = 1
我没有发布一些子工厂,但是如果需要的话可以。我不想让事情变得不清楚。
答案 0 :(得分:1)
您的PokemonGenderRatioFactory
默认为其外键创建一个新的Pokemon
。您想要的是一个指向您刚刚创建的口袋妖怪的东西:
def setUp(self):
""" Set Up for testing """
self.pokemon = PokemonFactory(species__evolves_from_species=None)
PokemonGenderRatioFactory(
pokemon=self.pokemon, # <-- this should be necessary
)