Commit fe0b633574b7fd8de4249755ab435cb41bdfc737
1 parent
fddbe196
Exists in
master
and in
3 other branches
Converted the tests to use Shoulda and Factory Girl, removed old fixtures
Showing
15 changed files
with
281 additions
and
243 deletions
Show diff stats
.gitignore
test/TESTING
1 | -Testing | ||
2 | -============== | ||
3 | - | ||
4 | -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. | ||
5 | - | ||
6 | - | ||
7 | -MySQL | ||
8 | -======= | ||
9 | - | ||
10 | -1. Create a new Rails app. | ||
11 | -2. Install acts_as_follower as a plugin. | ||
12 | -3. Copy the database config within the plugin: | ||
13 | - cp test/database.yml.example test/database.yml | ||
14 | -4. Create a database as specified in test/database.yml. | ||
15 | -5. Run the tests: | ||
16 | - rake test | ||
17 | - | ||
18 | - | ||
19 | -SQLite3 | ||
20 | -======= | ||
21 | - | ||
22 | -1. Create a new Rails app. | ||
23 | -2. Install acts_as_follower as a plugin. | ||
24 | -3. Copy the database config within the plugin: | ||
25 | - cp test/database.yml.example test/database.yml | ||
26 | -4. Install the sqlite3 library (if you don't have it already): | ||
27 | - sudo gem install sqlite3-ruby | ||
28 | -5. Run the tests: | ||
29 | - DB=sqlite3 rake test | ||
30 | - | ||
31 | - | ||
32 | -Coverage | ||
33 | -======= | ||
34 | - | ||
35 | -Test coverage can be calculated using Rcov. Make sure you have the rcov gem installed. | ||
36 | - | ||
37 | -Again in the acts_as_follower directory: | ||
38 | - | ||
39 | -rake rcov:gen # For sqlite | ||
40 | - | ||
41 | -or: | ||
42 | - | ||
43 | -DB=mysql rake rcov:gen # For mysql | ||
44 | - | ||
45 | -The coverage will now be available in the test/coverage directory. | ||
46 | - | ||
47 | -rake rcov:clobber will delete the coverage directory. | 1 | +Testing |
2 | +============== | ||
3 | + | ||
4 | +Tests are written with Shoulda on top of Test::Unit and Factory Girl is used instead of fixtures. Tests are run using rake. | ||
5 | +Test can either be run against a MySQL database or the faster in-memory SQLite3. | ||
6 | + | ||
7 | + | ||
8 | +MySQL | ||
9 | +======= | ||
10 | + | ||
11 | +1. Create a new Rails app. | ||
12 | +2. Install acts_as_follower as a plugin. | ||
13 | +3. Copy the database config within the plugin: | ||
14 | + cp test/database.yml.example test/database.yml | ||
15 | +4. Create a database as specified in test/database.yml. | ||
16 | +5. Run the tests: | ||
17 | + rake test | ||
18 | + | ||
19 | + | ||
20 | +SQLite3 | ||
21 | +======= | ||
22 | + | ||
23 | +1. Create a new Rails app. | ||
24 | +2. Install acts_as_follower as a plugin. | ||
25 | +3. Copy the database config within the plugin: | ||
26 | + cp test/database.yml.example test/database.yml | ||
27 | +4. Install the sqlite3 library (if you don't have it already): | ||
28 | + sudo gem install sqlite3-ruby | ||
29 | +5. Run the tests: | ||
30 | + DB=sqlite3 rake test | ||
31 | + | ||
32 | + | ||
33 | +Coverage | ||
34 | +======= | ||
35 | + | ||
36 | +Test coverage can be calculated using Rcov. Make sure you have the rcov gem installed. | ||
37 | + | ||
38 | +Again in the acts_as_follower directory: | ||
39 | + | ||
40 | +rake rcov:gen # For mysql | ||
41 | + | ||
42 | +or: | ||
43 | + | ||
44 | +rake rcov:gen DB=sqlite3 # For sqlite | ||
45 | + | ||
46 | +The coverage will now be available in the test/coverage directory. | ||
47 | + | ||
48 | +rake rcov:clobber will delete the coverage directory. |
test/acts_as_followable_test.rb
1 | -require File.dirname(__FILE__) + '/test_helper' | ||
2 | - | ||
3 | -class ActsAsFollowableTest < Test::Unit::TestCase | ||
4 | - fixtures :users, :follows | ||
5 | - | ||
6 | - def test_instance_methods_should_be_defined | ||
7 | - assert users(:sam).respond_to?(:followers_count) | ||
8 | - assert users(:sam).respond_to?(:followers) | ||
9 | - assert users(:sam).respond_to?(:followed_by?) | ||
10 | - end | ||
11 | - | ||
12 | - def test_followers_should_return_number_of_followers | ||
13 | - assert_equal 0, users(:sam).followers_count | ||
14 | - assert_equal 1, users(:jon).followers_count | ||
15 | - end | ||
16 | - | ||
17 | - def test_followers_should_return_users | ||
18 | - assert_equal [], users(:sam).followers | ||
19 | - assert_equal [users(:sam)], users(:jon).followers | ||
20 | - end | ||
21 | - | ||
22 | - def test_followed_by_should_return_follower_status | ||
23 | - assert_equal true, users(:jon).followed_by?(users(:sam)) | ||
24 | - assert_equal false, users(:sam).followed_by?(users(:jon)) | ||
25 | - end | ||
26 | - | ||
27 | - def test_destroyed_followable_should_destroy_related_follows_records | ||
28 | - assert_difference "Follow.count && users(:sam).all_following.size", -1 do | ||
29 | - users(:jon).destroy | ||
30 | - end | ||
31 | - end | ||
32 | - | ||
33 | -end | 1 | +require File.dirname(__FILE__) + '/test_helper' |
2 | + | ||
3 | +class ActsAsFollowableTest < Test::Unit::TestCase | ||
4 | + | ||
5 | + context "instance methods" do | ||
6 | + setup do | ||
7 | + @sam = Factory(:sam) | ||
8 | + end | ||
9 | + | ||
10 | + should "be defined" do | ||
11 | + assert @sam.respond_to?(:followers_count) | ||
12 | + assert @sam.respond_to?(:followers) | ||
13 | + assert @sam.respond_to?(:followed_by?) | ||
14 | + end | ||
15 | + end | ||
16 | + | ||
17 | + context "acts_as_followable" do | ||
18 | + setup do | ||
19 | + @sam = Factory(:sam) | ||
20 | + @jon = Factory(:jon) | ||
21 | + @sam.follow(@jon) | ||
22 | + end | ||
23 | + | ||
24 | + context "followers_count" do | ||
25 | + should "return the number of followers" do | ||
26 | + assert_equal 0, @sam.followers_count | ||
27 | + assert_equal 1, @jon.followers_count | ||
28 | + end | ||
29 | + end | ||
30 | + | ||
31 | + context "followers" do | ||
32 | + should "return users" do | ||
33 | + assert_equal [], @sam.followers | ||
34 | + assert_equal [@sam], @jon.followers | ||
35 | + end | ||
36 | + end | ||
37 | + | ||
38 | + context "followed_by" do | ||
39 | + should "return_follower_status" do | ||
40 | + assert_equal true, @jon.followed_by?(@sam) | ||
41 | + assert_equal false, @sam.followed_by?(@jon) | ||
42 | + end | ||
43 | + end | ||
44 | + | ||
45 | + context "destroying a followable" do | ||
46 | + setup do | ||
47 | + @jon.destroy | ||
48 | + end | ||
49 | + | ||
50 | + should_change "Follow.count", :by => -1 | ||
51 | + should_change "@sam.all_following.size", :by => -1 | ||
52 | + end | ||
53 | + end | ||
54 | + | ||
55 | +end |
test/acts_as_follower_test.rb
1 | -require File.dirname(__FILE__) + '/test_helper' | ||
2 | - | ||
3 | -class ActsAsFollowerTest < Test::Unit::TestCase | ||
4 | - fixtures :users, :follows, :bands | ||
5 | - | ||
6 | - def test_instance_methods_should_be_defined | ||
7 | - assert users(:sam).respond_to?(:following?) | ||
8 | - assert users(:sam).respond_to?(:follow_count) | ||
9 | - assert users(:sam).respond_to?(:follow) | ||
10 | - assert users(:sam).respond_to?(:stop_following) | ||
11 | - assert users(:sam).respond_to?(:follows_by_type) | ||
12 | - assert users(:sam).respond_to?(:all_follows) | ||
13 | - end | ||
14 | - | ||
15 | - def test_following_should_returns_following_status | ||
16 | - assert_equal true, users(:sam).following?(users(:jon)) | ||
17 | - assert_equal false, users(:jon).following?(users(:sam)) | ||
18 | - end | ||
19 | - | ||
20 | - def test_follow_count_should_return_count_of_followed_users | ||
21 | - assert_equal 2, users(:sam).follow_count | ||
22 | - assert_equal 0, users(:jon).follow_count | ||
23 | - end | ||
24 | - | ||
25 | - def test_follow_should_create_relevant_follow_record | ||
26 | - assert_difference "Follow.count", 1 do | ||
27 | - assert_difference "users(:jon).follow_count", 1 do | ||
28 | - users(:jon).follow(users(:sam)) | ||
29 | - end | ||
30 | - end | ||
31 | - assert_equal users(:jon), Follow.last.follower | ||
32 | - assert_equal users(:sam), Follow.last.followable | ||
33 | - end | ||
34 | - | ||
35 | - def test_stop_following_should_create_relevant_follow_record | ||
36 | - assert_difference "Follow.count", -1 do | ||
37 | - assert_difference "users(:sam).follow_count", -1 do | ||
38 | - users(:sam).stop_following(users(:jon)) | ||
39 | - end | ||
40 | - end | ||
41 | - end | ||
42 | - | ||
43 | - def test_follows_by_type_should_return_only_requested_follows | ||
44 | - assert_equal [follows(:band)], users(:sam).follows_by_type('Band') | ||
45 | - assert_equal [follows(:user)], users(:sam).follows_by_type('User') | ||
46 | - end | ||
47 | - | ||
48 | - def test_all_follows_should_return_all_follows | ||
49 | - follows = users(:sam).all_follows | ||
50 | - assert_equal 2, follows.size | ||
51 | - assert follows.include?(follows(:band)) | ||
52 | - assert follows.include?(follows(:user)) | ||
53 | - assert_equal [], users(:jon).all_follows | ||
54 | - end | ||
55 | - | ||
56 | - def test_all_following_should_return_actual_followed_records | ||
57 | - following = users(:sam).all_following | ||
58 | - assert_equal 2, following.size | ||
59 | - assert following.include?(bands(:oasis)) | ||
60 | - assert following.include?(users(:jon)) | ||
61 | - assert_equal [], users(:jon).all_following | ||
62 | - end | ||
63 | - | ||
64 | - def test_following_by_type_should_return_only_requested_records | ||
65 | - assert_equal [bands(:oasis)], users(:sam).following_by_type('Band') | ||
66 | - assert_equal [users(:jon)], users(:sam).following_by_type('User') | ||
67 | - end | ||
68 | - | ||
69 | - def test_method_missing_should_call_following_by_type | ||
70 | - assert_equal [bands(:oasis)], users(:sam).following_bands | ||
71 | - assert_equal [users(:jon)], users(:sam).following_users | ||
72 | - end | ||
73 | - | ||
74 | - def test_method_missing_should_raise | ||
75 | - assert_raises (NoMethodError){ users(:sam).foobar } | ||
76 | - end | ||
77 | - | ||
78 | - def test_destroyed_follower_should_nullifys_related_follows_records | ||
79 | - assert_difference "Follow.count && users(:sam).following_users.size", -1 do | ||
80 | - users(:jon).destroy | ||
81 | - end | ||
82 | - end | ||
83 | - | ||
84 | -end | 1 | +require File.dirname(__FILE__) + '/test_helper' |
2 | + | ||
3 | +class ActsAsFollowerTest < Test::Unit::TestCase | ||
4 | + | ||
5 | + context "instance methods" do | ||
6 | + setup do | ||
7 | + @sam = Factory(:sam) | ||
8 | + end | ||
9 | + | ||
10 | + should "be defined" do | ||
11 | + assert @sam.respond_to?(:following?) | ||
12 | + assert @sam.respond_to?(:follow_count) | ||
13 | + assert @sam.respond_to?(:follow) | ||
14 | + assert @sam.respond_to?(:stop_following) | ||
15 | + assert @sam.respond_to?(:follows_by_type) | ||
16 | + assert @sam.respond_to?(:all_follows) | ||
17 | + end | ||
18 | + end | ||
19 | + | ||
20 | + context "acts_as_follower" do | ||
21 | + setup do | ||
22 | + @sam = Factory(:sam) | ||
23 | + @jon = Factory(:jon) | ||
24 | + @oasis = Factory(:oasis) | ||
25 | + @sam.follow(@jon) | ||
26 | + @sam.follow(@oasis) | ||
27 | + end | ||
28 | + | ||
29 | + context "following" do | ||
30 | + should "return following_status" do | ||
31 | + assert_equal true, @sam.following?(@jon) | ||
32 | + assert_equal false, @jon.following?(@sam) | ||
33 | + end | ||
34 | + | ||
35 | + should "return follow_count" do | ||
36 | + assert_equal 2, @sam.follow_count | ||
37 | + assert_equal 0, @jon.follow_count | ||
38 | + end | ||
39 | + end | ||
40 | + | ||
41 | + context "follow" do | ||
42 | + setup do | ||
43 | + @jon.follow(@sam) | ||
44 | + end | ||
45 | + | ||
46 | + should_change "Follow.count", :by => 1 | ||
47 | + should_change "@jon.follow_count", :by => 1 | ||
48 | + | ||
49 | + should "set the follower" do | ||
50 | + assert_equal @jon, Follow.last.follower | ||
51 | + end | ||
52 | + | ||
53 | + should "set the followable" do | ||
54 | + assert_equal @sam, Follow.last.followable | ||
55 | + end | ||
56 | + end | ||
57 | + | ||
58 | + context "stop_following" do | ||
59 | + setup do | ||
60 | + @sam.stop_following(@jon) | ||
61 | + end | ||
62 | + | ||
63 | + should_change "Follow.count", :by => -1 | ||
64 | + should_change "@sam.follow_count", :by => -1 | ||
65 | + end | ||
66 | + | ||
67 | + context "follows" do | ||
68 | + setup do | ||
69 | + @band_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'Band'", @sam.id, @oasis.id]) | ||
70 | + @user_follow = Follow.find(:first, :conditions => ["follower_id = ? and follower_type = 'User' and followable_id = ? and followable_type = 'User'", @sam.id, @jon.id]) | ||
71 | + end | ||
72 | + | ||
73 | + context "follows_by_type" do | ||
74 | + should "only return requested follows" do | ||
75 | + assert_equal [@band_follow], @sam.follows_by_type('Band') | ||
76 | + assert_equal [@user_follow], @sam.follows_by_type('User') | ||
77 | + end | ||
78 | + end | ||
79 | + | ||
80 | + context "all_follows" do | ||
81 | + should "return all follows" do | ||
82 | + assert_equal 2, @sam.all_follows.size | ||
83 | + assert @sam.all_follows.include?(@band_follow) | ||
84 | + assert @sam.all_follows.include?(@user_follow) | ||
85 | + assert_equal [], @jon.all_follows | ||
86 | + end | ||
87 | + end | ||
88 | + end | ||
89 | + | ||
90 | + context "all_following" do | ||
91 | + should "return the actual follow records" do | ||
92 | + assert_equal 2, @sam.all_following.size | ||
93 | + assert @sam.all_following.include?(@oasis) | ||
94 | + assert @sam.all_following.include?(@jon) | ||
95 | + assert_equal [], @jon.all_following | ||
96 | + end | ||
97 | + end | ||
98 | + | ||
99 | + context "following_by_type" do | ||
100 | + should "return only requested records" do | ||
101 | + assert_equal [@oasis], @sam.following_by_type('Band') | ||
102 | + assert_equal [@jon], @sam.following_by_type('User') | ||
103 | + end | ||
104 | + end | ||
105 | + | ||
106 | + context "method_missing" do | ||
107 | + should "call following_by_type" do | ||
108 | + assert_equal [@oasis], @sam.following_bands | ||
109 | + assert_equal [@jon], @sam.following_users | ||
110 | + end | ||
111 | + | ||
112 | + should "raise on no method" do | ||
113 | + assert_raises (NoMethodError){ @sam.foobar } | ||
114 | + end | ||
115 | + end | ||
116 | + | ||
117 | + context "destroying follower" do | ||
118 | + setup do | ||
119 | + @jon.destroy | ||
120 | + end | ||
121 | + | ||
122 | + should_change "Follow.count", :by => -1 | ||
123 | + should_change "@sam.follow_count", :by => -1 | ||
124 | + end | ||
125 | + end | ||
126 | + | ||
127 | +end |
test/fixtures/band.rb
test/fixtures/bands.yml
test/fixtures/follows.yml
@@ -1,13 +0,0 @@ | @@ -1,13 +0,0 @@ | ||
1 | -user: | ||
2 | - id: 1 | ||
3 | - followable_id: 1 | ||
4 | - followable_type: User | ||
5 | - follower_id: 2 | ||
6 | - follower_type: User | ||
7 | - | ||
8 | -band: | ||
9 | - id: 2 | ||
10 | - followable_id: 1 | ||
11 | - followable_type: Band | ||
12 | - follower_id: 2 | ||
13 | - follower_type: User | ||
14 | \ No newline at end of file | 0 | \ No newline at end of file |
test/fixtures/user.rb
test/fixtures/users.yml
test/follow_test.rb
1 | -require File.dirname(__FILE__) + '/test_helper' | ||
2 | - | ||
3 | -class FollowTest < Test::Unit::TestCase | ||
4 | - | ||
5 | - # Replace with real tests | ||
6 | - def test_assert_true_should_be_true | ||
7 | - assert true | ||
8 | - end | ||
9 | - | ||
10 | -end | 1 | +require File.dirname(__FILE__) + '/test_helper' |
2 | + | ||
3 | +class FollowTest < Test::Unit::TestCase | ||
4 | + | ||
5 | + # Replace with real tests | ||
6 | + def test_assert_true_should_be_true | ||
7 | + assert true | ||
8 | + end | ||
9 | + | ||
10 | +end |
test/test_helper.rb
1 | -require 'test/unit' | ||
2 | -require 'rubygems' | ||
3 | - | ||
4 | -begin | ||
5 | - require File.dirname(__FILE__) + '/../../../../config/boot' | ||
6 | - require 'active_record' | ||
7 | -rescue LoadError | ||
8 | - require 'rubygems' | ||
9 | - require_gem 'activerecord' | ||
10 | -end | ||
11 | - | ||
12 | -require 'active_record/fixtures' | ||
13 | - | ||
14 | -require File.dirname(__FILE__) + '/../init.rb' | ||
15 | - | ||
16 | -ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') | ||
17 | -ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) | ||
18 | -ActiveRecord::Base.establish_connection(ENV['DB'] || 'mysql') | ||
19 | - | ||
20 | -load(File.dirname(__FILE__) + '/schema.rb') | ||
21 | - | ||
22 | -Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + '/fixtures/' | ||
23 | -$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path) | ||
24 | - | ||
25 | -class Test::Unit::TestCase #:nodoc: | ||
26 | - self.use_transactional_fixtures = true | ||
27 | - self.use_instantiated_fixtures = false | ||
28 | - | ||
29 | - def setup | ||
30 | - # Put any additional setup here. | ||
31 | - end | ||
32 | - | ||
33 | -end | 1 | +require 'rubygems' |
2 | +require 'active_record' | ||
3 | +require 'active_record/base' | ||
4 | + | ||
5 | +require File.dirname(__FILE__) + '/../init.rb' | ||
6 | +require File.dirname(__FILE__) + '/models/band' | ||
7 | +require File.dirname(__FILE__) + '/models/user' | ||
8 | + | ||
9 | +require 'test/unit' | ||
10 | +require 'shoulda' | ||
11 | +require 'factory_girl' | ||
12 | + | ||
13 | +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') | ||
14 | +ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) | ||
15 | +ActiveRecord::Base.establish_connection(ENV['DB'] || 'mysql') | ||
16 | + | ||
17 | +load(File.dirname(__FILE__) + '/schema.rb') |