Hey everyone,
at the end of the Automated Testing section, Mosh gives an Exercise to implement some tests for Products.
However, i dont find a solution nowhere for that.
The problem is that, for creating a product, i dont want write all the code and details to create a product. thats why i used the model_bakery. still, the
test_if_data_is_valid_returns_201
This is what i started to implement:
import pytest
from model_bakery import baker
from rest_framework import status
from store.models import Product
@pytest.fixture
def create_product(api_client):
product = baker.make(Product)
product.description = ''
return api_client.post('/store/products/', product.__dict__)
@pytest.mark.django_db
class TestCreateProduct:
def test_if_user_is_anonymous_returns_401(self, create_product):
assert create_product.status_code == status.HTTP_401_UNAUTHORIZED
def test_if_data_is_valid_returns_201(self, authenticate, create_product):
authenticate(is_staff=True)
assert create_product.status_code == status.HTTP_201_CREATED
assert create_product.data['id'] > 0
this here is the error i get on the console:
============================================================================== FAILURES ==============================================================================
________________________________________________________ TestCreateProduct.test_if_data_is_valid_returns_201 _________________________________________________________
self = <test_products.TestCreateProduct object at 0x7f509bf76b30>, authenticate = <function authenticate.<locals>.do_authenticate at 0x7f509b87f9a0>
create_product = <Response status_code=401, "application/json">
def test_if_data_is_valid_returns_201(self, authenticate, create_product):
authenticate(is_staff=True)
> assert create_product.status_code == status.HTTP_201_CREATED
E assert 401 == 201
E + where 401 = <Response status_code=401, "application/json">.status_code
E + and 201 = status.HTTP_201_CREATED
store/tests/test_products.py:23: AssertionError
did anybody find a nice solution for that?