Say I have models User
and Post
, a user has_many
posts and a post belongs_to
a user.
When I write a spec for Post
, my first instinct is to write something like this:
before do
@user = FactoryGirl.create :user
@post = @user.posts.new(title: "Foo", content: "bar)
end
... tests for @post go here ...
But this is going to create a new User - hitting the database - for every single test, which is going to slow things down. Is there a better way to do this that will speed my tests up and avoid hitting the DB so often?
As I understand it, I can't use FactoryGirl.build :user
because, even though it won't hit the DB, the associations won't work properly because @user
won't have an ID and so @post.user
won't work (it returns nil
.)
I could use FactoryGirl.build_stubbed :user
which created a "fake persisted" @user
which does have an ID, but @post.user
still returns nil. Does build_stubbed
have any practical advantage over build
when I'm testing things related to associations?
I suppose I could use build_stubbed
stub @post.user
so it returns @user
... is there any reason this might be a bad idea?
Or should I just use create
and accept the speed hit?
The only other alternative I can think of would be to set up @user in a before(:all)
block which seems like a bad idea.
What's the best way to write these kind of tests in a clean, concise way that avoids making too many DB queries?