Search code examples
ruby-on-railsrubyactiverecordrspec-railsrspec2

RSpec record created seemingly before(:suite) how do I track this record down?


Our test suite has a problem.

Whenever the whole suite starts up, a stray record is created.

Whenever I run a single file or context of specs, this stray record does not get created.

I wrote config.before(:each) { puts "COUNT = #{Model.count}\n" } in spec_helper.rb and no matter which order the tests ran, this record persists somehow. Seemingly before the examples even started.

We seed our database, so I tried a clean setup.

RAILS_ENV=test rake db:setup
RAILS_ENV=test rake db:seed
echo "Model.count" | rails c test
=> 0

Then whenever I run all tests (checked the order and checked for before(:all)).

rspec
COUNT = 1
.
COUNT = 1
.
COUNT = 1
.
etc

I've meticulously examined spec_helper.rb (RSpec2, pre rails_helper.rb) and commented out every support file with no luck.

I'm suspecting the code base at this point, maybe some weird call to something where someone left behind a find_or_create, a weird callback or something, I have no idea.

What I would like to know is:

  • How do I tackle breaking down the suites startup?
  • Can I get a backtrace of my test suite just starting up?
  • Anyone successfully chased down this kind of persistent record in RSpec?

EDIT

I added config.before(:suite) { debugger } and the record is still created before this!

How can I break down this code even further?

[23, 32] in /Users/yourname/.rvm/gems/ruby-1.9.3-p327@projectname/gems/rspec-core-2.14.6/lib/rspec/core/command_line.rb
   23          @world.announce_filters
   24
   25          @configuration.reporter.report(@world.example_count, @configuration.randomize? ? @configuration.seed : nil) do |reporter|
   26            begin
   27              @configuration.run_hook(:before, :suite)
=> 28              @world.example_groups.ordered.map {|g| g.run(reporter)}.all? ? 0 : @configuration.failure_exit_code
   29            ensure
   30              @configuration.run_hook(:after, :suite)
   31            end
   32          end
(rdb:1) ModelName.count
1

Solution

  • For the record of how I found the persistent record:

    By using before(:suite) { debugger }, I removed the record and ran the entire test suite and notice a test was failing.

    Within this test, was something similar to the following:

    context "blah" do
        model = FactoryGirl.create(:model)
    end
    

    I simply put the code within a before(:each) block, and the test was passing and now so were mine.

    TIL: Whenever RSpec loads the suite, it evaluates all the code that is not within a transaction. Which is why I could not try to pin point a problematic file by looking at when the record was being created.