Created
October 15, 2011 02:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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