public
Last active — forked from coreyhaines/.rspec

Active Record Spec Helper - Loading just active record

  • Download Gist
.rspec
1 2
--colour
-I app
active_record_spec_helper.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require 'active_record'
require 'database_cleaner'
 
connection_info = YAML.load_file("config/database.yml")["test"]
ActiveRecord::Base.establish_connection(connection_info)
 
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
coderetreat.rb
Ruby
1 2 3 4 5
class Coderetreat < ActiveRecord::Base
def self.running_today
where(scheduled_on: Date.today)
end
end
coderetreat_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
require 'active_record_spec_helper'
require 'models/coderetreat'
 
describe Coderetreat do
describe ".running_today" do
it "returns a coderetreat scheduled for today" do
coderetreat = Coderetreat.create! city: "Chicago", scheduled_on: Date.today
Coderetreat.running_today.all.should =~ [coderetreat]
end
 
it "does not return a coderetreat not scheduled for today" do
coderetreat = Coderetreat.create! city: "Chicago", scheduled_on: Date.today.advance(:days => -1)
Coderetreat.running_today.should be_empty
end
end
end
spec_helper.rb
Ruby
1 2 3 4 5 6 7 8 9
#Some databases get upset if you try to start a new transaction while a transaction is already in play, so running the whole spec suite chokes when rspec is trying to start a transaction. You need to update your spec_helper to rely on active_record_spec_helper to do this for you.
 
#Replace this line
config.use_transactional_fixtures = true
 
#With this
require 'active_record_spec_helper'
 
#TADA

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.