Rails Fixtures not loading with rspec
If you define fixtures as 'users', then the way to use them is via the method with the same name:
describe PagesController do
integrate_views
fixtures :users
it "should render index template on index call when logged in" do
session[:user_id] = users(:foo).id
get 'index'
response.should render_template('index')
end
end
The singular is only relevant to the class itself (User). Hope you still have some hair left if this is just a one letter bug.
Fixtures not loading in Rails test console
The fixture I had above was fine I just need to load the fixtures into the test database first with rails db:fixtures:load RAILS_ENV=test
Rails and Cucumber with Fixtures is not loading changes in Fixtures
Ok, maybe it was a silly question, but I would like to share the answer in case anyone else face this scenario in the future.
The answer is in the fixtures itself. They are created under spec
directory, which means they are part of Rspec
. Cucumber uses it but he doesn't reload them. So the way to see the changes reflected in Cucumber is simple executing Rspec
, he will make the reloads.
rspec
or
bundle exec rspec
works to see the changes reflected in Cucumber.
RSpec fixtures not loading
Maybe this is done automatically now and you have solved it, but have you checked that your "spec/spec_helper.rb" contains:
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
Cheers
Nil Associations with Rails Fixtures... how to fix?
Problem with fixtures
Looking at what you've done, locations(:loc_paris)
will find the record described in locations.yml, but locations(:loc_paris).retailer
won't.
Rails Associations work like this:
locations(:loc_paris).retailer
will look for the retailer
with retailer_id
mentioned in locations(:loc_paris)
record. In your case retailer_id: 399879241
and there is no reseller
with this id
that's why it returns Nil
.
Solution:
Describe fixtures like this:
#spec/fixtures/locations.yml
loc_paris:
retailer_id: 1
name: "Paris"
nickname: "paris"
loc_washington:
retailer_id: 2
name: "Washington"
nickname: "washington"
#spec/fixtures/retailers.yml
ret_europe:
id: 1
name: "AcmeCo France"
nickname: "acmecofr"
currency_type: "EUR"
ret_usa:
id: 2
name: "AcmeCo USA"
nickname: "acmecousa"
currency_type: "USD"
Now, locations(:loc_paris).retailer
will look for the retailer with retailer_id
mentioned in locations(:loc_paris)
record i.e. retailer_id: 1
and there is a reseller ret_europe
with this id
. Problem Solved
When you run rspec, at first rspec
saves these fixtures into your database with some auto-generated id
values (if id
not provided explicitly), that's why id
and reseller_id
are some random values. If you don't want the id
of locations.yml
record to be some random value, you can provide it yourself like this:
loc_paris:
id: 1
retailer_id: 1
name: "Paris"
nickname: "paris"
Tips:
As rspec
runs in test
environment (mentioned in app/spec/rails_helper.rb) and as I mentioned earlier whenever you run rspec
, at first it saves the fixtures into your database. If your local
and test
database are same, fixtures will replace the actual database records of your database. In your case, records in locations
and resellers
table record will be completely erased and replaced with these fixtures. So, make different database for test
environment.
Hope this answer is helpful
RSpec 2 doesn't seem to load any fixtures?
Try this:
ft = FundType.find_by_name("cash")
puts ft
puts ft.id
Verify your fixtures are in the directory:
ls -1 db/fixtures
Verify your configuration:
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/db/fixtures"
config.use_transactional_fixtures = true
config.global_fixtures = :all
end
Credit to the page that was previously here:
http://www.etagwerker.com/2011/08/how-to-load-all-fixtures-testing-with-rspec-2-and-rails-3/
Verify your test database comes up correctly:
rake db:test:prepare
Then look at the test database. Did the fixtures get loaded?
How to load the rspec fixtures in to the database from the files
For database fixtures being available before the application boots, you need to execute this rake task:
RAILS_ENV=test FIXTURES_PATH="spec/fixtures" rails db:fixtures:load
Related Topics
Can't Get Rack-Cors Working in Rails Application
How to Run Rails Console in the Test Environment and Load Test_Helper.Rb
One Liner in Ruby for Displaying a Prompt, Getting Input, and Assigning to a Variable
How Mix in Routes in Sinatra for a Better Structure
How to Use "Puts" to the Console Without a Line Break in Ruby on Rails
Embedding JSON Data into Yaml File
Hashes of Hashes Idiom in Ruby
Ruby on Rails: Devise, Want to Add Invite Code
Rspec: How to Test If a Method Was Called
Ruby on Rails Advanced JSON Serialization
How to Get Rspec-2 to Give the Full Trace Associated with a Test Failure
How to Use Unicorn as "Rails S"
How to Create a Full Audit Log in Rails for Every Table
Does Ruby Have the Java Equivalent of Synchronize Keyword
Ruby: What's the Difference Between Stdin.Gets() and Gets.Chomp()