Skip to content

Commit

Permalink
Merge pull request #4 from loganhasson/add-test-suite
Browse files Browse the repository at this point in the history
Add RSpec test suite and relevant development dependencies
  • Loading branch information
pcreux committed May 17, 2014
2 parents 848278e + d5ddd16 commit 1669d43
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
*.sqlite3
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format documentation
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in rspec-set.gemspec
gemspec
gemspec
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require "bundler/gem_tasks"
require "bundler/gem_tasks"
4 changes: 4 additions & 0 deletions rspec-set.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 2.14.1"
spec.add_development_dependency "database_cleaner"
spec.add_development_dependency "activerecord"
spec.add_development_dependency "sqlite3"
end
8 changes: 8 additions & 0 deletions spec/db/migrate/01_create_active_record_class_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateActiveRecordClassExample < ActiveRecord::Migration
def change
create_table :active_record_class_examples do |t|
t.string :name
t.integer :age
end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/active_record_class_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ActiveRecordClassExample < ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions spec/fixtures/non_active_record_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class NonActiveRecordClass
end
79 changes: 79 additions & 0 deletions spec/rspec_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'spec_helper'

describe 'including Set' do
it 'adds the ::set method to RSpec::Core::ExampleGroup' do
expect(RSpec::Core::ExampleGroup).to respond_to(:set)
end
end

describe 'without an ActiveRecord model' do
before do
@orig_stderr = $stderr
$stderr = StringIO.new
end

set(:my_object) { NonActiveRecordClass.new }

after do
$stderr = @orig_stderr
end

it "warns the user that Set only works with AR models" do
$stderr.rewind
expect($stderr.string.chomp).to eq(
"my_object is a NonActiveRecordClass - rspec-set works with ActiveRecord models only"
)
end
end

describe 'with an ActiveRecord model' do
before do
@orig_stderr = $stderr
$stderr = StringIO.new
end

set(:my_ar_object) do
ActiveRecordClassExample.create!(name: 'Person', age: 25)
end

after do
$stderr = @orig_stderr
end

it "doesn't give a warning to the user" do
$stderr.rewind
expect($stderr.string.chomp).to be_empty
end

it 'creates a method based on the argument to ::set' do
expect(self).to respond_to(:my_ar_object)
end
end

describe 'with a destroyed ActiveRecord model' do
set(:my_destroyed_object) do
ActiveRecordClassExample.create!(name: 'Alfred', age: 77)
end

it 'reloads a destroyed model' do
my_destroyed_object.destroy
expect(my_destroyed_object.persisted?).to be_false
end
end

describe 'with a stale model' do
set(:my_stale_object) do
ActiveRecordClassExample.create!(name: 'Old Name', age: 18)
end

it 'allows us to play with the model' do
my_stale_object.update(name: 'New Name')
expect(ActiveRecordClassExample.find(my_stale_object.id).name).to eq(
'New Name'
)
end

it 'reloads the stale model' do
expect(my_stale_object.name).to eq('Old Name')
end
end
58 changes: 58 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'rspec-set'
require 'database_cleaner'
require 'active_record'

require_relative './fixtures/non_active_record_class'
require_relative './fixtures/active_record_class_example'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

config.before(:suite) do
setup_database
end

config.after(:suite) do
destroy_database
end

config.before(:each) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end

config.order = 'random'
end

def db
ActiveRecord::Base.connection
end

def setup_database
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'spec/db/rspec-set-test.sqlite3'
)

db.tables.each do |table|
db.execute("DROP TABLE #{table}")
end

Dir[File.join(File.dirname(__FILE__), "db/migrate", "*.rb")].each do |f|
require f
migration = Kernel.const_get(f.split("/").last.split(".rb").first.gsub(/\d+/, "").split("_").collect{|w| w.strip.capitalize}.join())
migration.migrate(:up)
end
end

def destroy_database
db.tables.each do |table|
db.execute("DROP TABLE #{table}")
end
end

0 comments on commit 1669d43

Please sign in to comment.