Skip to content

Commit

Permalink
Updating tests to work with latest shoulda
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel Lindsaar committed Dec 21, 2010
1 parent 47b3ebc commit ab050fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
36 changes: 28 additions & 8 deletions test/factory_data_test.rb
Expand Up @@ -7,50 +7,68 @@ def setup

context 'Calling FactoryData.preload(:users)' do
setup do
@user_count = User.count
FactoryData.preload(:users) do |data|
data.add(:thom) { User.create(:first_name => 'Thom', :last_name => 'York') }
end
end

should_not_change 'User.count'
should_change "FactoryData.methods.include?('users')", :from => false, :to => true
should "not change User.count" do
assert_equal(@user_count, User.count)
end

should "change FactoryData.methods.include?('users') to true" do
assert_equal(true, FactoryData.methods.include?('users'))
end

context 'when there was a previous user record in the database' do
setup { User.create(:first_name => 'Barack', :last_name => 'Obama') }

context 'and calling FactoryData.delete_preload_data!' do
setup { FactoryData.delete_preload_data! }
should_change 'User.count', :to => 0

should "change User.count to 0" do
assert_equal(0, User.count)
end
end
end

context 'and later calling FactoryData.preload_data!' do
setup do
@user_count = User.count
@out, @err = OutputCapturer.capture do
FactoryData.preload_data!
end
end

should_change 'User.count', :by => 1
should "change User.count by 1" do
assert_equal(@user_count + 1, User.count)
end

context 'and later re-defining the preloaders' do
setup do
@user_count = User.count
FactoryData.preload(:users) do |data|
data.add(:thom) { User.create(:first_name => 'Thom', :last_name => 'York') }
data.add(:john) { User.create(:first_name => 'John', :last_name => 'Doe') }
end
end

should_change 'User.count', :by => -1
should "change User.count by - 1" do
assert_equal(@user_count - 1, User.count)
end

context 'and preloading the re-defined preloader' do
setup do
@user_count = User.count
@out, @err = OutputCapturer.capture do
FactoryData.preload_data!
end
end

should_change 'User.count', :by => 2
should "create 2 users" do
assert_equal(@user_count + 2, User.count)
end
end
end

Expand All @@ -76,10 +94,12 @@ def setup
end

context 'and later calling FactoryData.reset_cache!' do
setup { FactoryData.reset_cache! }
setup do
FactoryData.reset_cache!
end

should 'reload the record from the database the next time FactoryData.users(key) is called' do
User.expects(:find).once.returns(@user)
User.expects(:find_by_id).once.returns(@user)
FactoryData.users(:thom)
end
end
Expand Down
10 changes: 8 additions & 2 deletions test/preloader_test.rb
Expand Up @@ -20,12 +20,15 @@ def setup

context 'when preloaded' do
setup do
@user_count = User.count
@out, @err = OutputCapturer.capture do
@preloader.preload!
end
end

should_change 'User.count', :by => 2
should 'change User.count by 2' do
assert_equal(@user_count + 2, User.count)
end

should 'return the preloaded data when #get_record is called' do
assert_equal 'York', @preloader.get_record(:thom).last_name
Expand All @@ -39,6 +42,7 @@ def setup

context 'when preloaded again' do
setup do
@user_count = User.count
@out, @err = OutputCapturer.capture do
@preloader.preload!
end
Expand All @@ -49,7 +53,9 @@ def setup
assert_equal '', @out
end

should_not_change 'User.count'
should 'not change User.count' do
assert_equal(@user_count, User.count)
end
end

should 'issue a delete statement if #delete_table_data! is called' do
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'sqlite3'

begin
require 'ruby-debug'
Expand Down

0 comments on commit ab050fd

Please sign in to comment.