-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from loganhasson/add-test-suite
Add RSpec test suite and relevant development dependencies
- Loading branch information
Showing
10 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
*.sqlite3 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format documentation |
This file contains 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
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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require "bundler/gem_tasks" | ||
require "bundler/gem_tasks" |
This file contains 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
This file contains 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
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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class ActiveRecordClassExample < ActiveRecord::Base | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class NonActiveRecordClass | ||
end |
This file contains 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
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 |
This file contains 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
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 |