Speeding up associations in model specs with FactoryGirl - create vs build vs build_stubbed

GMA

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?

usha

If you don't want your tests to be hitting the database, this is what you would have to do.

before do
  @user = FactoryGirl.build_stubbed :user
  @post = FactoryGirl.build_stubbed :post
  @user.stub(:posts).and_return([@post])
  @post.stub(:user).and_return(@user)
end

Note: Be careful when using before(:all). It doesn't get executed in a transaction. So whatever you create in before(:all) will get left behind in the database and might cause conflict with other tests

About FactoryGirl.build, it builds the object, but creates the associations.

For eg:

factory :user do
  association posts
end

FactoryGirl.build(:user) #this creates posts in the database even though you are only building the parent object(user)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Speeding up the Dojo Build

From Dev

FactoryGirl build_stubbed & RSpec - Generates ID but fails to find id when testing Show action

From Dev

FactoryGirl Model Spec with Associations

From Dev

Build vs Create in has many through relationship

From Dev

Factory Girl attribute create vs build

From Dev

Methods for speeding up build time in a project using bitbake?

From Dev

Suites vs Specs Protractor

From Dev

How can I clean up this FactoryGirl build strategy?

From Dev

Build Flow vs Build Pipeline

From Dev

Build Flow vs Build Pipeline

From Dev

Gradle build VS eclipse build

From Dev

FactoryGirl Associations

From Java

Version vs build in Xcode

From Dev

No build notifications with VS 2015?

From Dev

FactoryGirl not passing arguments in build

From Dev

create a model instance with associations

From Dev

create a model instance with associations

From Java

Django Model() vs Model.objects.create()

From Dev

django - speeding up model object creation

From Dev

FactoryGirl and RSpec: create a record with required nested_attributes for specs

From Dev

Build VS 2015 extension on build server without VS installed?

From Java

Webpack build vs react-scripts build

From Dev

Difference between "Build Selection" and "Build Solution" in VS

From Dev

Release build vs. Debug build performance

From Dev

new Event('build') vs new CustomEvent('build')

From Dev

Profile Build vs Normal Build: CPU Usage?

From Dev

Profile Build vs Normal Build: CPU Usage?

From Dev

Yocto - development build vs production build

From Dev

VS Cordova IOS Remote Build