APIView 的 GET api 测试用例在 djangorestframework 中不起作用

Test case for GET api for APIView not working in djangorestframework

我正在 GET API 上用 Django 编写测试用例。我只想让第一个测试通过。这是我的代码。

class InventoryItemDetailTestCase(APITestCase):
    """
        This is the API test case for the reserve/express detail api
    """
    def setUp(self):
        self.resource_uri = '/api/v2/inventory/inventory_item_detail/1233/'

    def test_inventory_item_detail_route(self):
        """
            Test to set routing for item_detail
        """
        route = resolve('/api/v2/inventory/inventory_item_detail/1233/')
        self.assertEqual(route.func.__name__, 'InventoryItemDetails')

    def test_inventory_item_detail_data(self):
        """
            Test case to check the response json of the reserve/express API
        """
        response = self.client.get('/api/v2/inventory/inventory_item_detail/1233/')
        self.assertEqual(response.status_code, 200)

这里我使用client.get来请求。但它给了我错误

Creating test database for alias 'default'...

F

FAIL: test_inventory_item_detail_data (inventory.tests.functional_tests.InventoryItemDetailTestCase)

Traceback (most recent call last): File "/Users/chitrankdixit/Documents/work/flyrobe/flyrobe-django/project/inventory/tests/functional_tests.py", line 131, in >test_inventory_item_detail_data self.assertEqual(response.status_code, 200) AssertionError: 400 != 200


Ran 1 test in 0.135s

FAILED (failures=1)

我尝试使用 pdb.set_trace() 来弄清楚会发生什么错误,当我 运行

self.client.get('/api/v2/inventory/inventory_item_detail/1233/')

我收到这个错误

*** KeyError: 'content-type'

我试过提供一个额外的参数名称 content_type 比如

self.client.get('/api/v2/inventory/inventory_item_detail/1233/', content_type='application/json')

但我仍然遇到同样的错误。我可以 运行 单独 API 并且我的 API 正在获取正确的 responses.If 有人已经经历过这个,请让我知道。

传递给 client.get 的 headers 的名称应遵循 CGI specification. From docs:

The headers sent via **extra should follow CGI specification. For example, emulating a different “Host” header as sent in the HTTP request from the browser to the server should be passed as HTTP_HOST.

也就是说,您应该指定 HTTP_CONTENT_TYPE 而不是 content_type

我通过测试解决了这个问题,我没有制作测试用例中使用的所需模型的实例,所以我只需要更新 setUp() 方法,现在我的设置方法看起来有点像像这样。

def setUp(self):
        self.warehouse = Warehouse.objects.create(
            location_name = 'Mumbai',
            address = 'D1/D2 tex center Chandivali',
        )
        self.supplier = Supplier.objects.create(
            name = "John Doe",
            website = "johndoe@johndoe.com",
            address = "USA, San francisco , CA"
        )
        self.procurement_detail = ProcurementDetail.objects.create(
            invoice_name = "Indore Mill",
            invoice_number = "14235",
            details = "Cloth from Central India",
            total_cost_price = "0.00",
            payment_method = "CASH",
            supplier_id = self.supplier.id,
            is_paid = "True"
        )
        self.inventory_category = InventoryCategory.objects.create(
            name = "party time"
        )
        self.color = Color.objects.create(
            name = "Green",
            hex_val = "#007089"
        )
        self.occasion = Occasion.objects.create(
            name = "BDay"
        )
        self.photo_shoot = PhotoShoot.objects.create(
            name = 'front 1',
            details = 'This is the front facing image',
            model_size = {'name':'abcd'}
        )
        self.brand = Brand.objects.create(
            name = "ZARA1"
        )
        self.parent_item_description = ParentItemDescription.objects.create(
            features = 'Indian clothing at its best',
            style_tip = 'This is style tip',
            fitting_notes = 'This is fitting notes',
            long_description = 'This is the Long description'
        )
        self.parent_item = ParentItem.objects.create(
            parent_id = "1003",
            mrp = "0.00",
            photo_shoot_id = self.photo_shoot.id,
            description_id = self.parent_item_description.id,
            cost_price = "0.00",
            rental_price = "0.00",
            discount_price = "0.00",
            discount_percent = "0.00",
            security_deposit = "0.00",
            material_percentage = '"cotton"=>"20"',
            mode = "Express",
            brand_id = self.brand.id,
            features = "THe best feature the best it is ",
            size = ['S','M','L'],
            online_link_url = "http://chitrank-dixit.github.io/",
            visibility_order_index = 1,
            category = 'Express clothing',
            inventory_category_id = self.inventory_category.id,
            popularity_index = 90,
            item_domain = "CLT",
            gender = "FML"
        )
        self.inventory_item = InventoryItem.objects.create(
            warehouse_id = self.warehouse.id,
            parent_item_id = self.parent_item.id,
            size = ['S','M','L'],
            procurement_detail_id = self.procurement_detail.id,
            size_range_id = 2,
            product_tag = {'name':'abcd'},
            short_description = 'abcd',
            is_virtual = False,
            warehouse_stack_id = "12345",
            reason_for_out_stock = "NA",
            status = "ILV"
        )
        self.inventory_item.color.add(self.color)
        self.inventory_item.occasion.add(self.occasion)
        self.resource_url = '/api/v2/inventory/inventory_item_detail/'+str(self.inventory_item.parent_item.parent_id)+'/'

现在可以在下面定义的测试用例中使用设置中创建的实例。