Commit bb5da52eddea74a3d6720243a98ee5fdd3d304bf

Authored by Leandro Santos
1 parent ff2a47e2

Rails3: Fix ProfileCategorizationTest unit tests

app/models/category.rb
1 class Category < ActiveRecord::Base 1 class Category < ActiveRecord::Base
2 2
  3 + attr_accessible :name, :parent_id
  4 +
3 SEARCHABLE_FIELDS = { 5 SEARCHABLE_FIELDS = {
4 :name => 10, 6 :name => 10,
5 :acronym => 5, 7 :acronym => 5,
app/models/environment.rb
@@ -183,6 +183,8 @@ class Environment &lt; ActiveRecord::Base @@ -183,6 +183,8 @@ class Environment &lt; ActiveRecord::Base
183 183
184 has_many :product_categories, :conditions => { :type => 'ProductCategory'} 184 has_many :product_categories, :conditions => { :type => 'ProductCategory'}
185 has_many :regions 185 has_many :regions
  186 + has_many :states
  187 + has_many :cities
186 188
187 has_many :roles 189 has_many :roles
188 190
test/unit/profile_categorization_test.rb
@@ -13,8 +13,10 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -13,8 +13,10 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
13 end 13 end
14 14
15 should 'create instances for the entire hierarchy' do 15 should 'create instances for the entire hierarchy' do
16 - c1 = Category.create!(:name => 'c1', :environment => Environment.default)  
17 - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) 16 + c1 = Environment.default.categories.create!(:name => 'c1')
  17 + c2 = Environment.default.categories.create!(:name => 'c2').tap do |c|
  18 + c.parent_id = c1.id
  19 + end
18 20
19 p = create_user('testuser').person 21 p = create_user('testuser').person
20 22
@@ -26,9 +28,13 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -26,9 +28,13 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
26 end 28 end
27 29
28 should 'not duplicate entry for category that is parent of two others' do 30 should 'not duplicate entry for category that is parent of two others' do
29 - c1 = Category.create!(:name => 'c1', :environment => Environment.default)  
30 - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default)  
31 - c3 = c1.children.create!(:name => 'c3', :environment => Environment.default) 31 + c1 = Environment.default.categories.create!(:name => 'c1')
  32 + c2 = Environment.default.categories.create!(:name => 'c2').tap do |c|
  33 + c.parent_id = c1.id
  34 + end
  35 + c3 = Environment.default.categories.create!(:name => 'c3').tap do |c|
  36 + c.parent_id = c1.id
  37 + end
32 38
33 p = create_user('testuser').person 39 p = create_user('testuser').person
34 40
@@ -39,9 +45,13 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -39,9 +45,13 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
39 end 45 end
40 46
41 should 'remove all instances for a given profile' do 47 should 'remove all instances for a given profile' do
42 - c1 = Category.create!(:name => 'c1', :environment => Environment.default)  
43 - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default)  
44 - c3 = c1.children.create!(:name => 'c3', :environment => Environment.default) 48 + c1 = Environment.default.categories.create!(:name => 'c1')
  49 + c2 = Environment.default.categories.create!(:name => 'c2').tap do |c|
  50 + c.parent_id = c1.id
  51 + end
  52 + c3 = Environment.default.categories.create!(:name => 'c3').tap do |c|
  53 + c.parent_id = c1.id
  54 + end
45 55
46 p = create_user('testuser').person 56 p = create_user('testuser').person
47 57
@@ -55,7 +65,7 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase @@ -55,7 +65,7 @@ class ProfileCategorizationTest &lt; ActiveSupport::TestCase
55 65
56 [ Region, State, City ].each do |klass| 66 [ Region, State, City ].each do |klass|
57 should "be able to remove #{klass.name} from profile" do 67 should "be able to remove #{klass.name} from profile" do
58 - region = klass.create!(:name => 'my region', :environment => Environment.default) 68 + region = Environment.default.send(klass.name.underscore.pluralize).create!(:name => 'my region')
59 p = create_user('testuser').person 69 p = create_user('testuser').person
60 p.region = region 70 p.region = region
61 p.save! 71 p.save!