From d5ddd164ab3dec36fcb396df31e7f49e3f2c823c Mon Sep 17 00:00:00 2001 From: Logan Hasson Date: Fri, 16 May 2014 22:16:25 -0400 Subject: [PATCH] Add RSpec test suite and relevant development dependencies --- .gitignore | 1 + .rspec | 2 + Gemfile | 2 +- Rakefile | 2 +- rspec-set.gemspec | 4 + .../01_create_active_record_class_example.rb | 8 ++ spec/fixtures/active_record_class_example.rb | 2 + spec/fixtures/non_active_record_class.rb | 2 + spec/rspec_set_spec.rb | 79 +++++++++++++++++++ spec/spec_helper.rb | 58 ++++++++++++++ 10 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 .rspec create mode 100644 spec/db/migrate/01_create_active_record_class_example.rb create mode 100644 spec/fixtures/active_record_class_example.rb create mode 100644 spec/fixtures/non_active_record_class.rb create mode 100644 spec/rspec_set_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index d87d4be..9667a91 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ spec/reports test/tmp test/version_tmp tmp +*.sqlite3 \ No newline at end of file diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..b3eb8b4 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation \ No newline at end of file diff --git a/Gemfile b/Gemfile index 848b3fa..e9a117f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in rspec-set.gemspec -gemspec +gemspec \ No newline at end of file diff --git a/Rakefile b/Rakefile index 2995527..13a79ed 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1 @@ -require "bundler/gem_tasks" +require "bundler/gem_tasks" \ No newline at end of file diff --git a/rspec-set.gemspec b/rspec-set.gemspec index a8f27ab..4a02061 100644 --- a/rspec-set.gemspec +++ b/rspec-set.gemspec @@ -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 diff --git a/spec/db/migrate/01_create_active_record_class_example.rb b/spec/db/migrate/01_create_active_record_class_example.rb new file mode 100644 index 0000000..4588398 --- /dev/null +++ b/spec/db/migrate/01_create_active_record_class_example.rb @@ -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 \ No newline at end of file diff --git a/spec/fixtures/active_record_class_example.rb b/spec/fixtures/active_record_class_example.rb new file mode 100644 index 0000000..255ef23 --- /dev/null +++ b/spec/fixtures/active_record_class_example.rb @@ -0,0 +1,2 @@ +class ActiveRecordClassExample < ActiveRecord::Base +end \ No newline at end of file diff --git a/spec/fixtures/non_active_record_class.rb b/spec/fixtures/non_active_record_class.rb new file mode 100644 index 0000000..efd35a5 --- /dev/null +++ b/spec/fixtures/non_active_record_class.rb @@ -0,0 +1,2 @@ +class NonActiveRecordClass +end \ No newline at end of file diff --git a/spec/rspec_set_spec.rb b/spec/rspec_set_spec.rb new file mode 100644 index 0000000..81eb966 --- /dev/null +++ b/spec/rspec_set_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..74becc5 --- /dev/null +++ b/spec/spec_helper.rb @@ -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 \ No newline at end of file