Skip to content

Instantly share code, notes, and snippets.

@revans
Forked from tenderlove/.rspec
Created December 3, 2012 17:04
Show Gist options
  • Save revans/4196367 to your computer and use it in GitHub Desktop.
Save revans/4196367 to your computer and use it in GitHub Desktop.
Active Record Spec Helper - Loading just active record
require 'active_record'
# MiniTest Version
connection_info = YAML.load_file("config/database.yml")["test"]
ActiveRecord::Base.establish_connection(connection_info)
class MiniTest::Spec
after(:each) do
ActiveRecord::Base.subclasses.each(&:delete_all)
end
end
class Coderetreat < ActiveRecord::Base
def self.running_today
where(scheduled_on: Date.today)
end
end
require 'active_record_test_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.must_match [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.must_be_empty
end
end
end
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment