Is there a simple way in factory girl to create a new factory only if one doesn't already exist?
If there isn't a simple way, what's the most concise means to ensure only one factory is created for a set of cucumber features (and/or specs)?
For example, I need a single (common) administrator record in a 'user' model to test multiple cucumber features. Ideally I'd like to do it without wrapping conditionals around every create admin step, but without hitting the 'record already exists' error.
Any suggestions appreciated.
Create a helper method to either create or return a singleton instance.
def create_or_return_admin_user @user ||= Factory(:user, :admin => true) end
and then call
create_or_return_admin_user
in your test.
You can't do that in Factory_girl only, you need to create a method checking if the record exist or if it's not in your database.
If you do that in the setup ( before Rspec ) you can be sure that there is only one record.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With