Rspec 'hides' records? #6

Closed
jamesw opened this Issue Jul 3, 2011 · 3 comments

Projects

None yet

2 participants

@jamesw

Using RSpec Rails with factory girl in a Rails 3.0.7 app
I have a very contrived example to illustrate the issue I'm facing
given three models - Parent, Child and Country

a parent has many children

child has a default_scope as follows

default_scope where("country_id = ?", Country.default_country_id)

The Country model has a default_country_id method that looks like this

def self.default_country_id
  result = Country.first.country_id unless self.first.blank? #There are no records in the database in RSpec tests when issued by the default scope, however there are records when tested directly
end

The test looks like this and ALL tests PASS but the passing condition is wrong for the @children.size.should == 0 I only changed it to 0 from 2 to see what happened to the other tests that come after this and to prove the point that at one stage RSpec believes that the Country record with a country_id of 1 exists but at a later point the record does not exist and after that point the record comes back again making it look like the record is hidden somehow and then becomes visible again

  it "Has many english children" do
    @country = Factory(:country, :country_id => 1, :name => 'England')
    @parent = Factory(:parent)
    @child_1 = Factory(:child, :country_id => 1)
    @child_2 = Factory(:child, :country_id => 1)
    @child_3 = Factory(:child, :country_id => 2)
    Country.default_country_id == 1
    @children = @parent.children
    @children.size.should == 0 #This passes but it really should have 2 records however the default scope on the Child model shows that NULL is used in the SQL statement
    @country.count.should == 1
    @country.name.should == "English"
    Country.count.should == 1
    @country.default_country_id.should == 1 #And it does so how comes the default scope fails to find it?
  end

Like I say that is a very contrived example but it illustrates the issue I have been scratching my head over for a week now.
The code works fine in the console and in the app it's only a problem with RSPect (or maybe my configuration of RSPec which is as follows
Gemfile
group :development, :test do
gem 'rspec-rails', "~> 2.4"
gem 'capybara'
gem 'launchy'
gem 'factory_girl_rails'
end

spec_helper.rb looks like this

This file is copied to spec/ when you run 'rails generate rspec:install'

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", FILE)
require 'rspec/rails'
require "controller_macros"

Requires supporting ruby files with custom matchers and macros, etc,

in spec/support/ and its subdirectories.

Dir[Rails.root.join("spec/support/*/.rb")].each {|f| require f}

RSpec.configure do |config|
config.mock_with :rspec

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
config.include(ControllerMacros)
end

This is a model test and I am running
rake spec:models
and
rake spec
Makes no difference
Also I have only just created the default_scope on the child model. I originally had that call as a :conditions => ["country_id = ?", Country.default_country_id] for the parent has_many :children declaration
I only moved it to a default_scope on the child model so I could be certain that there was not something wrong with my has_many declaration and to see if it made a difference to RSpec

The code I have actually woks perfectly in the console and in the app but I've got 29 failing real life tests because of this one issue.

Could it be that this is an RSpec bug? or is it that I need to configure something that I am not aware of?

I can rule out a factory girl bug as I have done a direct create of the country record in the spec to the same effect

I also had those Factory records created as part of a before_each but moved them to better understand the test.log entries when looking for what was happening with the SQL

@dchelimsky
Owner

This is the tracker for rspec-rails-1 (w/ rails-2). Would you kindly re-post as/is to https://github.com/rspec/rspec-rails/issues? Thx.

@dchelimsky dchelimsky closed this Jul 3, 2011
@dchelimsky
Owner

I got a head start on looking at this and there are a number of problems and inconsistencies in the example you posted, including :country_id v :default_country_id (in the Country model), class level v instance level :default_country_id (in the Country model), 'England' v 'English', etc.

If you do repost this in the other tracker, please be sure to include all the code that I need to run to see the exact failure you are seeing.

Thx.

@jamesw

Sorry for the inconsistencies, and thank you for looking at this. I'll repost with corrected code.
Cheers.

James

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment