我遇到了让这个测试工作的问题。这是github https://github.com/cbaldwin20/project_9/blob/master/menu/tests.py上的整个文件。谢谢你的任何建议。
这是测试
user = User.objects.get(id=1)
def test_edit_item_view_post(self):
self.client.post(reverse('mysite:item_edit', args=[self.item.id]), {
'name': 'Ketchup',
'description': 'Heinz brand',
'chef': user,
'ingredients': ['1']
})
item1 = Item.objects.get(id=1)
self.assertEqual(item1.name, 'Ketchup')
以下是我正在测试的视图
def edit_item(request, pk):
item = get_object_or_404(Item, pk=pk)
if request.method == "POST":
form = ItemForm(instance=item, data=request.POST)
if form.is_valid():
form.save()
return redirect('mysite:item_detail', pk=pk)
form = ItemForm(instance=item)
return render(request, 'menu/item_edit.html', {'form': form})
这是表格
class ItemForm(forms.ModelForm):
class Meta:
model = Item
exclude = ('created_date',)
这是表单使用的模型
class Item(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
chef = models.ForeignKey('auth.User')
created_date = models.DateTimeField(
default=timezone.now)
standard = models.BooleanField(default=False)
ingredients = models.ManyToManyField('Ingredient')