From fe0b633574b7fd8de4249755ab435cb41bdfc737 Mon Sep 17 00:00:00 2001 From: Tom Cocca Date: Thu, 30 Apr 2009 20:47:03 -0400 Subject: [PATCH] Converted the tests to use Shoulda and Factory Girl, removed old fixtures --- .gitignore | 9 +++++---- test/TESTING | 95 ++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------- test/acts_as_followable_test.rb | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- test/acts_as_follower_test.rb | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------ test/factories/bands.rb | 3 +++ test/factories/users.rb | 7 +++++++ test/fixtures/band.rb | 4 ---- test/fixtures/bands.yml | 3 --- test/fixtures/follows.yml | 13 ------------- test/fixtures/user.rb | 5 ----- test/fixtures/users.yml | 7 ------- test/follow_test.rb | 20 ++++++++++---------- test/models/band.rb | 4 ++++ test/models/user.rb | 5 +++++ test/test_helper.rb | 50 +++++++++++++++++--------------------------------- 15 files changed, 281 insertions(+), 243 deletions(-) create mode 100644 test/factories/bands.rb create mode 100644 test/factories/users.rb delete mode 100644 test/fixtures/band.rb delete mode 100644 test/fixtures/bands.yml delete mode 100644 test/fixtures/follows.yml delete mode 100644 test/fixtures/user.rb delete mode 100644 test/fixtures/users.yml create mode 100644 test/models/band.rb create mode 100644 test/models/user.rb diff --git a/.gitignore b/.gitignore index 3431d83..c1f0a34 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -test/debug.log -coverage -rdoc -test/database.yml +test/debug.log +coverage +rdoc +test/database.yml +memory \ No newline at end of file diff --git a/test/TESTING b/test/TESTING index ab8a57c..02a21d5 100644 --- a/test/TESTING +++ b/test/TESTING @@ -1,47 +1,48 @@ -Testing -============== - -Tests are written with the standard Rails setup of Test::Unit and run using rake. Test can either be run against a MySQL database or the faster in-memory SQLite3. - - -MySQL -======= - -1. Create a new Rails app. -2. Install acts_as_follower as a plugin. -3. Copy the database config within the plugin: - cp test/database.yml.example test/database.yml -4. Create a database as specified in test/database.yml. -5. Run the tests: - rake test - - -SQLite3 -======= - -1. Create a new Rails app. -2. Install acts_as_follower as a plugin. -3. Copy the database config within the plugin: - cp test/database.yml.example test/database.yml -4. Install the sqlite3 library (if you don't have it already): - sudo gem install sqlite3-ruby -5. Run the tests: - DB=sqlite3 rake test - - -Coverage -======= - -Test coverage can be calculated using Rcov. Make sure you have the rcov gem installed. - -Again in the acts_as_follower directory: - -rake rcov:gen # For sqlite - -or: - -DB=mysql rake rcov:gen # For mysql - -The coverage will now be available in the test/coverage directory. - -rake rcov:clobber will delete the coverage directory. +Testing +============== + +Tests are written with Shoulda on top of Test::Unit and Factory Girl is used instead of fixtures. Tests are run using rake. +Test can either be run against a MySQL database or the faster in-memory SQLite3. + + +MySQL +======= + +1. Create a new Rails app. +2. Install acts_as_follower as a plugin. +3. Copy the database config within the plugin: + cp test/database.yml.example test/database.yml +4. Create a database as specified in test/database.yml. +5. Run the tests: + rake test + + +SQLite3 +======= + +1. Create a new Rails app. +2. Install acts_as_follower as a plugin. +3. Copy the database config within the plugin: + cp test/database.yml.example test/database.yml +4. Install the sqlite3 library (if you don't have it already): + sudo gem install sqlite3-ruby +5. Run the tests: + DB=sqlite3 rake test + + +Coverage +======= + +Test coverage can be calculated using Rcov. Make sure you have the rcov gem installed. + +Again in the acts_as_follower directory: + +rake rcov:gen # For mysql + +or: + +rake rcov:gen DB=sqlite3 # For sqlite + +The coverage will now be available in the test/coverage directory. + +rake rcov:clobber will delete the coverage directory. diff --git a/test/acts_as_followable_test.rb b/test/acts_as_followable_test.rb index f742b69..7bb5086 100644 --- a/test/acts_as_followable_test.rb +++ b/test/acts_as_followable_test.rb @@ -1,33 +1,55 @@ -require File.dirname(__FILE__) + '/test_helper' - -class ActsAsFollowableTest < Test::Unit::TestCase - fixtures :users, :follows - - def test_instance_methods_should_be_defined - assert users(:sam).respond_to?(:followers_count) - assert users(:sam).respond_to?(:followers) - assert users(:sam).respond_to?(:followed_by?) - end - - def test_followers_should_return_number_of_followers - assert_equal 0, users(:sam).followers_count - assert_equal 1, users(:jon).followers_count - end - - def test_followers_should_return_users - assert_equal [], users(:sam).followers - assert_equal [users(:sam)], users(:jon).followers - end - - def test_followed_by_should_return_follower_status - assert_equal true, users(:jon).followed_by?(users(:sam)) - assert_equal false, users(:sam).followed_by?(users(:jon)) - end - - def test_destroyed_followable_should_destroy_related_follows_records - assert_difference "Follow.count && users(:sam).all_following.size", -1 do - users(:jon).destroy - end - end - -end +require File.dirname(__FILE__) + '/test_helper' + +class ActsAsFollowableTest < Test::Unit::TestCase + + context "instance methods" do + setup do + @sam = Factory(:sam) + end + + should "be defined" do + assert @sam.respond_to?(:followers_count) + assert @sam.respond_to?(:followers) + assert @sam.respond_to?(:followed_by?) + end + end + + context "acts_as_followable" do + setup do + @sam = Factory(:sam) + @jon = Factory(:jon) + @sam.follow(@jon) + end + + context "followers_count" do + should "return the number of followers" do + assert_equal 0, @sam.followers_count + assert_equal 1, @jon.followers_count + end + end + + context "followers" do + should "return users" do + assert_equal [], @sam.followers + assert_equal [@sam], @jon.followers + end + end + + context "followed_by" do + should "return_follower_status" do + assert_equal true, @jon.followed_by?(@sam) + assert_equal false, @sam.followed_by?(@jon) + end + end + + context "destroying a followable" do + setup do + @jon.destroy + end + + should_change "Follow.count", :by => -1 + should_change "@sam.all_following.size", :by => -1 + end + end + +end diff --git a/test/acts_as_follower_test.rb b/test/acts_as_follower_test.rb index c9a6451..6b407af 100644 --- a/test/acts_as_follower_test.rb +++ b/test/acts_as_follower_test.rb @@ -1,84 +1,127 @@ -require File.dirname(__FILE__) + '/test_helper' - -class ActsAsFollowerTest < Test::Unit::TestCase - fixtures :users, :follows, :bands - - def test_instance_methods_should_be_defined - assert users(:sam).respond_to?(:following?) - assert users(:sam).respond_to?(:follow_count) - assert users(:sam).respond_to?(:follow) - assert users(:sam).respond_to?(:stop_following) - assert users(:sam).respond_to?(:follows_by_type) - assert users(:sam).respond_to?(:all_follows) - end - - def test_following_should_returns_following_status - assert_equal true, users(:sam).following?(users(:jon)) - assert_equal false, users(:jon).following?(users(:sam)) - end - - def test_follow_count_should_return_count_of_followed_users - assert_equal 2, users(:sam).follow_count - assert_equal 0, users(:jon).follow_count - end - - def test_follow_should_create_relevant_follow_record - assert_difference "Follow.count", 1 do - assert_difference "users(:jon).follow_count", 1 do - users(:jon).follow(users(:sam)) - end - end - assert_equal users(:jon), Follow.last.follower - assert_equal users(:sam), Follow.last.followable - end - - def test_stop_following_should_create_relevant_follow_record - assert_difference "Follow.count", -1 do - assert_difference "users(:sam).follow_count", -1 do - users(:sam).stop_following(users(:jon)) - end - end - end - - def test_follows_by_type_should_return_only_requested_follows - assert_equal [follows(:band)], users(:sam).follows_by_type('Band') - assert_equal [follows(:user)], users(:sam).follows_by_type('User') - end - - def test_all_follows_should_return_all_follows - follows = users(:sam).all_follows - assert_equal 2, follows.size - assert follows.include?(follows(:band)) - assert follows.include?(follows(:user)) - assert_equal [], users(:jon).all_follows - end - - def test_all_following_should_return_actual_followed_records - following = users(:sam).all_following - assert_equal 2, following.size - assert following.include?(bands(:oasis)) - assert following.include?(users(:jon)) - assert_equal [], users(:jon).all_following - end - - def test_following_by_type_should_return_only_requested_records - assert_equal [bands(:oasis)], users(:sam).following_by_type('Band') - assert_equal [users(:jon)], users(:sam).following_by_type('User') - end - - def test_method_missing_should_call_following_by_type - assert_equal [bands(:oasis)], users(:sam).following_bands - assert_equal [users(:jon)], users(:sam).following_users - end - - def test_method_missing_should_raise - assert_raises (NoMethodError){ users(:sam).foobar } - end - - def test_destroyed_follower_should_nullifys_related_follows_records - assert_difference "Follow.count && users(:sam).following_users.size", -1 do - users(:jon).destroy - end - end - -end +require File.dirname(__FILE__) + '/test_helper' + +class ActsAsFollowerTest < Test::Unit::TestCase + + context "instance methods" do + setup do + @sam = Factory(:sam) + end + + should "be defined" do + assert @sam.respond_to?(:following?) + assert @sam.respond_to?(:follow_count) + assert @sam.respond_to?(:follow) + assert @sam.respond_to?(:stop_following) + assert @sam.respond_to?(:follows_by_type) + assert @sam.respond_to?(:all_follows) + end + end + + context "acts_as_follower" do + setup do + @sam = Factory(:sam) + @jon = Factory(:jon) + @oasis = Factory(:oasis) + @sam.follow(@jon) + @sam.follow(@oasis) + end + + context "following" do + should "return following_status" do + assert_equal true, @sam.following?(@jon) + assert_equal false, @jon.following?(@sam) + end + + should "return follow_count" do + assert_equal 2, @sam.follow_count + assert_equal 0, @jon.follow_count + end + end + + context "follow" do + setup do + @jon.follow(@sam) + end + + should_change "Follow.count", :by => 1 + should_change "@jon.follow_count", :by => 1 + + should "set the follower" do + assert_equal @jon, Follow.last.follower + end + + should "set the followable" do + assert_equal @sam, Follow.last.followable + end + end + + context "stop_following" do + setup do + @sam.stop_following(@jon) + end + + should_change "Follow.count", :by => -1 + should_change "@sam.follow_count", :by => -1 + end + + context "follows" do + setup do + @band_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id]) + @user_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id]) + end + + context "follows_by_type" do + should "only return requested follows" do + assert_equal [@band_follow], @sam.follows_by_type('Band') + assert_equal [@user_follow], @sam.follows_by_type('User') + end + end + + context "all_follows" do + should "return all follows" do + assert_equal 2, @sam.all_follows.size + assert @sam.all_follows.include?(@band_follow) + assert @sam.all_follows.include?(@user_follow) + assert_equal [], @jon.all_follows + end + end + end + + context "all_following" do + should "return the actual follow records" do + assert_equal 2, @sam.all_following.size + assert @sam.all_following.include?(@oasis) + assert @sam.all_following.include?(@jon) + assert_equal [], @jon.all_following + end + end + + context "following_by_type" do + should "return only requested records" do + assert_equal [@oasis], @sam.following_by_type('Band') + assert_equal [@jon], @sam.following_by_type('User') + end + end + + context "method_missing" do + should "call following_by_type" do + assert_equal [@oasis], @sam.following_bands + assert_equal [@jon], @sam.following_users + end + + should "raise on no method" do + assert_raises (NoMethodError){ @sam.foobar } + end + end + + context "destroying follower" do + setup do + @jon.destroy + end + + should_change "Follow.count", :by => -1 + should_change "@sam.follow_count", :by => -1 + end + end + +end diff --git a/test/factories/bands.rb b/test/factories/bands.rb new file mode 100644 index 0000000..48492c8 --- /dev/null +++ b/test/factories/bands.rb @@ -0,0 +1,3 @@ +Factory.define :oasis, :class => Band do |b| + b.name 'Oasis' +end diff --git a/test/factories/users.rb b/test/factories/users.rb new file mode 100644 index 0000000..5cffb80 --- /dev/null +++ b/test/factories/users.rb @@ -0,0 +1,7 @@ +Factory.define :jon, :class => User do |u| + u.name 'Jon' +end + +Factory.define :sam, :class => User do |u| + u.name 'Sam' +end diff --git a/test/fixtures/band.rb b/test/fixtures/band.rb deleted file mode 100644 index 7f94535..0000000 --- a/test/fixtures/band.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Band < ActiveRecord::Base - validates_presence_of :name - acts_as_followable -end diff --git a/test/fixtures/bands.yml b/test/fixtures/bands.yml deleted file mode 100644 index da27ffa..0000000 --- a/test/fixtures/bands.yml +++ /dev/null @@ -1,3 +0,0 @@ -oasis: - id: 1 - name: Oasis \ No newline at end of file diff --git a/test/fixtures/follows.yml b/test/fixtures/follows.yml deleted file mode 100644 index b746676..0000000 --- a/test/fixtures/follows.yml +++ /dev/null @@ -1,13 +0,0 @@ -user: - id: 1 - followable_id: 1 - followable_type: User - follower_id: 2 - follower_type: User - -band: - id: 2 - followable_id: 1 - followable_type: Band - follower_id: 2 - follower_type: User \ No newline at end of file diff --git a/test/fixtures/user.rb b/test/fixtures/user.rb deleted file mode 100644 index ab286bd..0000000 --- a/test/fixtures/user.rb +++ /dev/null @@ -1,5 +0,0 @@ -class User < ActiveRecord::Base - validates_presence_of :name - acts_as_follower - acts_as_followable -end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml deleted file mode 100644 index 63155e4..0000000 --- a/test/fixtures/users.yml +++ /dev/null @@ -1,7 +0,0 @@ -jon: - id: 1 - name: Jon - -sam: - id: 2 - name: Sam diff --git a/test/follow_test.rb b/test/follow_test.rb index 258c461..6b63694 100644 --- a/test/follow_test.rb +++ b/test/follow_test.rb @@ -1,10 +1,10 @@ -require File.dirname(__FILE__) + '/test_helper' - -class FollowTest < Test::Unit::TestCase - - # Replace with real tests - def test_assert_true_should_be_true - assert true - end - -end +require File.dirname(__FILE__) + '/test_helper' + +class FollowTest < Test::Unit::TestCase + + # Replace with real tests + def test_assert_true_should_be_true + assert true + end + +end diff --git a/test/models/band.rb b/test/models/band.rb new file mode 100644 index 0000000..db90d74 --- /dev/null +++ b/test/models/band.rb @@ -0,0 +1,4 @@ +class Band < ActiveRecord::Base + validates_presence_of :name + acts_as_followable +end diff --git a/test/models/user.rb b/test/models/user.rb new file mode 100644 index 0000000..81f35b4 --- /dev/null +++ b/test/models/user.rb @@ -0,0 +1,5 @@ +class User < ActiveRecord::Base + validates_presence_of :name + acts_as_follower + acts_as_followable +end diff --git a/test/test_helper.rb b/test/test_helper.rb index fb190fd..1427b4a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,33 +1,17 @@ -require 'test/unit' -require 'rubygems' - -begin - require File.dirname(__FILE__) + '/../../../../config/boot' - require 'active_record' -rescue LoadError - require 'rubygems' - require_gem 'activerecord' -end - -require 'active_record/fixtures' - -require File.dirname(__FILE__) + '/../init.rb' - -ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') -ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) -ActiveRecord::Base.establish_connection(ENV['DB'] || 'mysql') - -load(File.dirname(__FILE__) + '/schema.rb') - -Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + '/fixtures/' -$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path) - -class Test::Unit::TestCase #:nodoc: - self.use_transactional_fixtures = true - self.use_instantiated_fixtures = false - - def setup - # Put any additional setup here. - end - -end +require 'rubygems' +require 'active_record' +require 'active_record/base' + +require File.dirname(__FILE__) + '/../init.rb' +require File.dirname(__FILE__) + '/models/band' +require File.dirname(__FILE__) + '/models/user' + +require 'test/unit' +require 'shoulda' +require 'factory_girl' + +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') +ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) +ActiveRecord::Base.establish_connection(ENV['DB'] || 'mysql') + +load(File.dirname(__FILE__) + '/schema.rb') -- libgit2 0.21.0