Commit 112531c1b6f41ca12da3664bbc9dac4d867108d7
1 parent
56661a84
Exists in
master
and in
22 other branches
rails3: fix ArticleCategorization unit tests
Showing
1 changed file
with
29 additions
and
15 deletions
Show diff stats
test/unit/article_categorization_test.rb
| ... | ... | @@ -9,17 +9,21 @@ class ArticleCategorizationTest < ActiveSupport::TestCase |
| 9 | 9 | should 'belong to article' do |
| 10 | 10 | p = create_user('testuser').person |
| 11 | 11 | article = p.articles.build(:name => 'test article'); article.save! |
| 12 | - assert_equal article, ArticleCategorization.new(:article => article).article | |
| 12 | + categorization = ArticleCategorization.new | |
| 13 | + categorization.article = article | |
| 14 | + assert_equal article, categorization.article | |
| 13 | 15 | end |
| 14 | 16 | |
| 15 | 17 | should 'belong to category' do |
| 16 | - category = Category.create!(:name => 'one category', :environment => Environment.default) | |
| 17 | - assert_equal category, ArticleCategorization.new(:category => category).category | |
| 18 | + category = create_category('one category') | |
| 19 | + categorization = ArticleCategorization.new | |
| 20 | + categorization.category = category | |
| 21 | + assert_equal category, categorization.category | |
| 18 | 22 | end |
| 19 | 23 | |
| 20 | 24 | should 'create instances for the entire hierarchy' do |
| 21 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | |
| 22 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | |
| 25 | + c1 = create_category('c1') | |
| 26 | + c2 = create_category('c2', c1) | |
| 23 | 27 | |
| 24 | 28 | p = create_user('testuser').person |
| 25 | 29 | a = p.articles.create!(:name => 'test') |
| ... | ... | @@ -32,9 +36,9 @@ class ArticleCategorizationTest < ActiveSupport::TestCase |
| 32 | 36 | end |
| 33 | 37 | |
| 34 | 38 | should 'not duplicate entry for category that is parent of two others' do |
| 35 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | |
| 36 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | |
| 37 | - c3 = c1.children.create!(:name => 'c3', :environment => Environment.default) | |
| 39 | + c1 = create_category('c1') | |
| 40 | + c2 = create_category('c2', c1) | |
| 41 | + c3 = create_category('c3', c1) | |
| 38 | 42 | |
| 39 | 43 | p = create_user('testuser').person |
| 40 | 44 | a = p.articles.create!(:name => 'test') |
| ... | ... | @@ -46,9 +50,9 @@ class ArticleCategorizationTest < ActiveSupport::TestCase |
| 46 | 50 | end |
| 47 | 51 | |
| 48 | 52 | should 'remove all instances for a given article' do |
| 49 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | |
| 50 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | |
| 51 | - c3 = c1.children.create!(:name => 'c3', :environment => Environment.default) | |
| 53 | + c1 = create_category('c1') | |
| 54 | + c2 = create_category('c2', c1) | |
| 55 | + c3 = create_category('c3', c1) | |
| 52 | 56 | |
| 53 | 57 | p = create_user('testuser').person |
| 54 | 58 | a = p.articles.create!(:name => 'test') |
| ... | ... | @@ -62,8 +66,8 @@ class ArticleCategorizationTest < ActiveSupport::TestCase |
| 62 | 66 | end |
| 63 | 67 | |
| 64 | 68 | should 'not duplicate when adding the parent of a category by witch the article is already categorized' do |
| 65 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | |
| 66 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | |
| 69 | + c1 = create_category('c1') | |
| 70 | + c2 = create_category('c2', c1) | |
| 67 | 71 | |
| 68 | 72 | p = create_user('testuser').person |
| 69 | 73 | a = p.articles.create!(:name => 'test') |
| ... | ... | @@ -75,8 +79,8 @@ class ArticleCategorizationTest < ActiveSupport::TestCase |
| 75 | 79 | end |
| 76 | 80 | |
| 77 | 81 | should 'make parent real when categorized after child' do |
| 78 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | |
| 79 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | |
| 82 | + c1 = create_category('c1') | |
| 83 | + c2 = create_category('c2', c1) | |
| 80 | 84 | |
| 81 | 85 | p = create_user('testuser').person |
| 82 | 86 | a = p.articles.create!(:name => 'test') |
| ... | ... | @@ -85,4 +89,14 @@ class ArticleCategorizationTest < ActiveSupport::TestCase |
| 85 | 89 | |
| 86 | 90 | assert ArticleCategorization.find(:first, :conditions => [ 'category_id = ? and article_id = ? and not virtual', c1.id, a.id ]), 'categorization must be promoted to not virtual' |
| 87 | 91 | end |
| 92 | + | |
| 93 | + private | |
| 94 | + | |
| 95 | + def create_category(name, parent = nil) | |
| 96 | + c = Category.new(:name => name) | |
| 97 | + c.environment = Environment.default | |
| 98 | + c.parent = parent | |
| 99 | + c.save! | |
| 100 | + c | |
| 101 | + end | |
| 88 | 102 | end | ... | ... |