Skip to content

Commit

Permalink
Improve tests and testing environment
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Jan 8, 2013
1 parent 2ebef78 commit 4002e85
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ wiki/
Gemfile.lock
.DS_Store
tmp/
.bundle
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
source :rubygems

gemspec

group :testing do
gem "pry"
end
4 changes: 0 additions & 4 deletions spec/sinatra/activerecord/rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ def schema_version
end

before(:each) do
FileUtils.mkdir_p("tmp")
FileUtils.rm_rf("tmp/foo.sqlite3")

ActiveRecord::Base.remove_connection
ActiveRecord::Base.establish_connection("sqlite3:///tmp/foo.sqlite3")
ActiveRecord::Migrator.migrations_paths = "tmp"
Expand All @@ -26,7 +23,6 @@ def schema_version

after(:each) do
FileUtils.rm_rf("db")
FileUtils.rm_rf("tmp")
end

it "uses ActiveRecord::Migrator.migrations_paths for the migration directory" do
Expand Down
25 changes: 6 additions & 19 deletions spec/sinatra/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ def new_sinatra_application
end

it "establishes the database connection when set" do
expect {
@app.set :database, database_url
}.to change{ActiveRecord::Base.connected?}.to(true)
expect {
@app.set :database, database_url
}.to change{ActiveRecord::Base.connection.last_use}
expect { @app.set :database, database_url }.to establish_database_connection
expect { @app.set :database, database_url }.to change{ActiveRecord::Base.connection.last_use}
end

it "can have the SQLite database in a folder" do
Expand All @@ -52,18 +48,13 @@ def new_sinatra_application
end

it "accepts a hash for the database" do
expect {
@app.set :database, {adapter: "sqlite3", database: "tmp/foo.sqlite3"}
}.to change{ActiveRecord::Base.connected?}.to(true)
expect { @app.set :database, {adapter: "sqlite3", database: "tmp/foo.sqlite3"} }.to establish_database_connection
end

describe "database file" do
it "accepts a filename for the database" do
FileUtils.mkdir_p("tmp")
FileUtils.cp("spec/fixtures/database.yml", "tmp")
expect {
@app.set :database_file, "database.yml"
}.to change{ActiveRecord::Base.connected?}.to(true)
expect { @app.set :database_file, "database.yml" }.to establish_database_connection
end

it "doesn't raise errors on missing #app_file" do
Expand All @@ -82,18 +73,14 @@ def new_sinatra_application

it "establishes the connection upon registering" do
ActiveRecord::Base.remove_connection
expect {
@app = new_sinatra_application
}.to change{ActiveRecord::Base.connected?}.to(true)
expect { @app = new_sinatra_application }.to establish_database_connection
end

it "is overriden by config/database.yml" do
FileUtils.mkdir_p("tmp/config")
FileUtils.touch("tmp/config/database.yml")

expect {
@app = new_sinatra_application
}.to raise_error(ActiveRecord::AdapterNotSpecified)
expect { @app = new_sinatra_application }.to raise_error(ActiveRecord::AdapterNotSpecified)
end
end
end
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
begin
require "pry"
rescue LoadError
end

ROOT = File.expand_path("../", File.dirname(__FILE__))
Dir[File.join(ROOT, "spec/support/**/*.rb")].each &method(:require)

RSpec.configure do |config|
config.before(:each) do
FileUtils.mkdir_p("tmp")
FileUtils.rm("tmp/foo.sqlite3") if File.exists?("tmp/foo.sqlite3")
end

config.after(:each) do
FileUtils.rm_rf("tmp")
end

config.include Helpers
end
5 changes: 5 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Helpers
def establish_database_connection
change{ActiveRecord::Base.connected?}.to(true)
end
end

0 comments on commit 4002e85

Please sign in to comment.