Skip to content

Instantly share code, notes, and snippets.

@developish
Created October 15, 2011 02:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developish/1288943 to your computer and use it in GitHub Desktop.
Save developish/1288943 to your computer and use it in GitHub Desktop.
How to create an isolated ActiveRecord connection for testing without breaking the rest of the suite.
require "active_record"
require "lib/duration"
class Event < ActiveRecord::Base
include Duration
end
describe Duration do
before do
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:")
ActiveRecord::Base.connection.create_table(:events) do |t|
t.datetime :start_at
t.datetime :end_at
end
end
# Return to your regularly scheduled testing
after :all do
if ActiveRecord::Base.configurations['test']
ActiveRecord::Base.establish_connection :test
end
end
describe "started/not_started" do
it "should return events with past start_at" do
event = Event.create :start_at => 1.day.ago
Event.started.should == [event]
Event.not_started.should == []
end
it "should not return events with future start_at" do
event = Event.create :start_at => 1.day.from_now
Event.started.should == []
Event.not_started.should == [event]
end
end
describe "ended/not_ended" do
it "should return events with past end_at" do
event = Event.create :start_at => 1.day.ago,
:end_at => 1.day.ago
Event.started.should == [event]
Event.not_started.should == []
end
it "should not return events with future end_at" do
event = Event.create :start_at => 1.day.ago,
:end_at => 1.day.from_now
Event.started.should == [event]
Event.not_started.should == []
end
end
describe "current" do
it "should return event that are started and not ended" do
event = Event.create :start_at => 1.day.ago,
:end_at => 1.day.from_now
Event.current.should == [event]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment