Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update spec suite and fix destroyed model reload bug #5

Merged
merged 1 commit into from
May 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/rspec-set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def set(variable_name, &block)

if model.is_a?(ActiveRecord::Base)
if model.destroyed?
# Relig destroyed model
model.class.find(i.id)
# Reload destroyed model
model.class.find(model.id)
elsif !model.new_record?
# Reload saved model
model.reload
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/non_active_record_class.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
class NonActiveRecordClass
attr_accessor :name

def initialize(attributes)
self.name = attributes[:name]
end

def self.create(attributes)
new(attributes)
end
end
140 changes: 113 additions & 27 deletions spec/rspec_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@
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
setup_for_error_checking(NonActiveRecordClass)

it "warns the user that Set only works with AR models" do
$stderr.rewind
Expand All @@ -27,43 +18,38 @@
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
setup_for_error_checking(ActiveRecordClassExample)

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)
expect(self).to respond_to(:my_object)
end
end

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

it 'reloads a destroyed model' do
it 'allows us to dstroy a model' do
my_destroyed_object.destroy
expect(my_destroyed_object.persisted?).to be_false
expect(
ActiveRecordClassExample.find_by(id: my_destroyed_object.id)
).to be_nil
end

it 'reloads a destroyed model' do
expect(my_destroyed_object.name).to eq('Alfred')
end
end

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

it 'allows us to play with the model' do
Expand All @@ -76,4 +62,104 @@
it 'reloads the stale model' do
expect(my_stale_object.name).to eq('Old Name')
end
end


describe ActiveRecordClassExample do
set(:ar_class_example) { ActiveRecordClassExample.create(name: 'ex_1') }

subject { ar_class_example }

context "when name is changed to 'ex_2" do
before do
ar_class_example.update(name: 'ex_2')
end

it 'updates the name' do
expect(subject.name).to eq('ex_2')
end
end

context "when name is 'ex_1" do
it 'reloads the original name' do
expect(subject.name).to eq('ex_1')
end
end
end

describe 'sub sub contexts' do
set(:ar_class_example) { ActiveRecordClassExample.create(name: 'apple') }

subject { ar_class_example }

context "when name is changed to 'banana'" do
before do
ar_class_example.update(name: 'banana')
end

it 'updates the name' do
expect(subject.name).to eq('banana')
end

context "when we append ' is good'" do
before do
ar_class_example.name << ' is good'
ar_class_example.save
end

it 'updates the appended name' do
expect(subject.name).to eq('banana is good')
end
end

context "when we append ' is bad'" do
before do
ar_class_example.name << ' is bad'
ar_class_example.save
end

it 'also updates the appended name' do
expect(subject.name).to eq('banana is bad')
end

context "when we append ' for you'" do
before do
ar_class_example.name << ' for you'
ar_class_example.save
end

it 'contains the full sentence' do
expect(subject.name).to eq('banana is bad for you')
end
end
end
end

context "when name is 'apple'" do
it 'reloads the original name' do
expect(subject.name).to eq('apple')
end
end
end

describe 'deleting an object' do
set(:ar_class_example) { ActiveRecordClassExample.create(name: 'apple') }

subject { ar_class_example }

context "when I destroy the ar_class_example" do
before do
ar_class_example.destroy
end

it "is destroyed" do
expect(ActiveRecordClassExample.find_by_id(ar_class_example.id)).to be_nil
end
end

context "when name is 'apple'" do
it 'is reloaded from the database' do
expect(subject.name).to eq('apple')
end
end
end
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,17 @@ def destroy_database
db.tables.each do |table|
db.execute("DROP TABLE #{table}")
end
end

def setup_for_error_checking(klass)
before do
@orig_stderr = $stderr
$stderr = StringIO.new
end

set(:my_object) { klass.create(name: 'Test Name') }

after do
$stderr = @orig_stderr
end
end