I ran into some frustrating issues while writing integration tests for an AngularJS app. My factory_girl records were not being sent to the client. After two days of struggling to figure this out, I decided to pass on integration tests for my own app. Then I ran into the same issue while pairing with my bud, Thomas Barnwell, on a Backbone app.

I soon realized that stubbing ActiveRecord at the controller level solved the problem but my pair was unsatisfied with stubbing so we continued exploring. We realized that our database cleaning strategy had everything to do with it, thanks to Avdi Grimm.

There are two ways around this issue:

1. Stub the ActiveRecord portion of your controller call

it "shows all of the posts" do
  Post.stub(:all) { [ post1, post2 ] }
  ...
end

The code above assumes that FactoryGirl was used to create post1 and post2. The controller returns the stubbed data as JSON.

2. Use database_cleaner and :truncation strategy for integration tests

Avdi sums this method up pretty good.

I’m not sure why but I had to stop my development server after making changes to spec_helper.rb before this would work.

I also completely removed this line from the spec_helper.rb after adding Avdi’s database_cleaner code.

 config.use_transactional_fixtures = true 

Avdi’s strategy assumes that you are using js: true to enable javascript and signal that you are writing an integration test.

For example:

describe "user creates a post", js: true do
  ...
end

Comments