Reputation: 341
I'm new to FactoryGirl, Faker, and more broadly testing.
I believe I need to add an ID to my user that is created by FactoryGirl in order to assess whether the user can access certain pages and/or has ownership over certain nested resources. I'm questioning this because as I do research(Google and check StackOverflow) I'm not seeing a lot of information about generating IDs for FactoryGirl created resources. Since my user comes with an ID of nil I assume the FactoryGirl default is to build a resource without adding an ID.
I do see information about creating associations, but even those seem to be absent ids. In the app I'm creating all access to a user's resources are checked against a user's ID.
Should I be creating and ID for my FactoryGirl generated users? If not, why not? What should I do instead? If so, how do I generate different user IDs to check against each other?
Thanks in advance for your input.
Upvotes: 0
Views: 869
Reputation: 411
The ID should be assigned/incremented automatically if you are persisting the user object. It's possible you're using build
to instantiate the object rather than create
.
user = build(:user)
<-- Object is created, not saved, no ID
user = create(:user)
<--- Object is created and saved, has ID
For best practices, ensure your tests are run on a separate database so that you don't pollute your live data. You could also check out https://github.com/DatabaseCleaner/database_cleaner for an easy way to clean up your test data after a test has been run.
Upvotes: 2