Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django测试:即使创建了匹配的查询,也没有匹配的查询_Python_Django_Database_Orm_Model - Fatal编程技术网

Python Django测试:即使创建了匹配的查询,也没有匹配的查询

Python Django测试:即使创建了匹配的查询,也没有匹配的查询,python,django,database,orm,model,Python,Django,Database,Orm,Model,运行manage.py测试时,我收到以下错误: ====================================================================== ERROR: test_pokemon_detail_view (pokedex.tests.test_views.TestViews) Test the pokemon detail view and ensure the correct template was used ----------------

运行
manage.py测试时,我收到以下错误:

======================================================================
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.

----------------------------------------------------------------------
这通常意味着数据库中没有pokemonderrations对象,但我在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')
有人知道为什么会出错吗?我试着用
--verbosity3
运行测试,但没有给出更多关于测试的细节

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)
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

有几个子工厂我没有发布,但如果需要可以发布。我不想让事情变得不清楚。

默认情况下,你的
pokemonderratiofactory
为其外键创建一个新的
Pokemon
。你想要的是一个指向你刚刚创建的口袋妖怪:

def setUp(self):
    """ Set Up for testing """
    self.pokemon = PokemonFactory(species__evolves_from_species=None)
    PokemonGenderRatioFactory(
        pokemon=self.pokemon,  # <-- this should be necessary
    )
def设置(自):
“”“为测试设置”“”
self.pokemon=PokemonFactory(物种从物种演化而来=无)
神奇宝贝(

pokemon=self.pokemon,#在
test\u pokemon\u detail\u view
中查看代码也会很有帮助。您使用的是哪种测试框架?您确定类正确调用了安装程序吗?如果您测试pokemongederrations.objects.count()?@schwobaseggl我添加了test_pokemon_细节_view@JulienKieffer我确信调用安装程序是因为我让它打印“Created xxx Object”调试消息。这是Django的内置测试,它使用unittest if IIRC.pokemonderrations.objects.count()是1。你就是那个人!就是这样。我不认为我会找到它,除非我看了《口袋妖怪.objects.count()》/code>并看到有2个。