I have two scenarios that create a record in a database. Let's say I'm creating a user with [email protected]
.
- Scenario 1: Given I have an user with email
[email protected]
- Scenario 2: Given I have an user with email
[email protected]
Scenario 2 is raising an error that says there is already a record with [email protected]
. I think that I need to setup a hook to cleanup database between scenarios.
What is best practice for this stuation? Calling flushdb
command between scenarios? Or transaction rollbacks? Or what else?
Use the
get_or_create
method in both scenarios. Don't forget thatget_or_create
returns a tuple:https://docs.djangoproject.com/en/1.8/ref/models/querysets/#get-or-create