Commit 29f6b68e5b527e4d0d94d4b4501b8e212cff723a
1 parent
ddcf8bea
Exists in
master
and in
27 other branches
rails3: fix enterprise tests
PS: still tests failin
Showing
2 changed files
with
40 additions
and
37 deletions
Show diff stats
app/models/enterprise.rb
... | ... | @@ -53,8 +53,9 @@ class Enterprise < Organization |
53 | 53 | super + FIELDS |
54 | 54 | end |
55 | 55 | |
56 | - def validate | |
57 | - super | |
56 | + validate :presence_of_required_fieds | |
57 | + | |
58 | + def presence_of_required_fieds | |
58 | 59 | self.required_fields.each do |field| |
59 | 60 | if self.send(field).blank? |
60 | 61 | self.errors.add_on_blank(field) |
... | ... | @@ -103,7 +104,7 @@ class Enterprise < Organization |
103 | 104 | if environment.replace_enterprise_template_when_enable |
104 | 105 | apply_template(template) |
105 | 106 | end |
106 | - save_without_validation! | |
107 | + save(:validate => false) | |
107 | 108 | end |
108 | 109 | |
109 | 110 | def question | ... | ... |
test/unit/enterprise_test.rb
... | ... | @@ -41,7 +41,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
41 | 41 | end |
42 | 42 | |
43 | 43 | def test_belongs_to_environment_and_has_default |
44 | - assert_equal Environment.default, Enterprise.create!(:name => 'my test environment', :identifier => 'mytestenvironment').environment | |
44 | + assert_equal Environment.default, create(Enterprise, :name => 'my test environment', :identifier => 'mytestenvironment').environment | |
45 | 45 | end |
46 | 46 | |
47 | 47 | def test_cannot_rename |
... | ... | @@ -62,8 +62,8 @@ class EnterpriseTest < ActiveSupport::TestCase |
62 | 62 | |
63 | 63 | should 'remove products when removing enterprise' do |
64 | 64 | e = fast_create(Enterprise, :name => "My enterprise", :identifier => 'myenterprise') |
65 | - e.products.create!(:name => 'One product', :product_category => @product_category) | |
66 | - e.products.create!(:name => 'Another product', :product_category => @product_category) | |
65 | + create(Product, :enterprise => e, :name => 'One product', :product_category => @product_category) | |
66 | + create(Product, :enterprise => e, :name => 'Another product', :product_category => @product_category) | |
67 | 67 | |
68 | 68 | assert_difference Product, :count, -2 do |
69 | 69 | e.destroy |
... | ... | @@ -71,15 +71,16 @@ class EnterpriseTest < ActiveSupport::TestCase |
71 | 71 | end |
72 | 72 | |
73 | 73 | should 'create a default set of articles' do |
74 | - Enterprise.any_instance.expects(:default_set_of_articles).returns([Blog.new(:name => 'Blog')]) | |
75 | - enterprise = Enterprise.create!(:name => 'my test enterprise', :identifier => 'myenterprise') | |
74 | + blog = build(Blog) | |
75 | + Enterprise.any_instance.expects(:default_set_of_articles).returns([blog]) | |
76 | + enterprise = create(Enterprise, :name => 'my test enterprise', :identifier => 'myenterprise') | |
76 | 77 | |
77 | - assert_kind_of Blog, enterprise.articles.find_by_path('blog') | |
78 | - assert_kind_of RssFeed, enterprise.articles.find_by_path('blog/feed') | |
78 | + assert_kind_of Blog, enterprise.articles.find_by_path(blog.path) | |
79 | + assert_kind_of RssFeed, enterprise.articles.find_by_path(blog.feed.path) | |
79 | 80 | end |
80 | 81 | |
81 | 82 | should 'create default set of blocks' do |
82 | - e = Enterprise.create(:name => 'my new community', :identifier => 'mynewcommunity') | |
83 | + e = create(Enterprise, :name => 'my new community', :identifier => 'mynewcommunity') | |
83 | 84 | |
84 | 85 | assert !e.boxes[0].blocks.empty?, 'person must have blocks in area 1' |
85 | 86 | assert !e.boxes[1].blocks.empty?, 'person must have blocks in area 2' |
... | ... | @@ -101,6 +102,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
101 | 102 | enterprise.add_member(member) |
102 | 103 | |
103 | 104 | person = fast_create(Person) |
105 | + enterprise.stubs(:notification_emails).returns(['sample@example.org']) | |
104 | 106 | enterprise.add_member(person) |
105 | 107 | |
106 | 108 | assert_equal false, person.is_member_of?(enterprise) |
... | ... | @@ -183,7 +185,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
183 | 185 | inactive_template.boxes << Box.new |
184 | 186 | inactive_template.save! |
185 | 187 | |
186 | - active_template = Enterprise.create!(:name => 'enteprise template', :identifier => 'enterprise_template') | |
188 | + active_template = create(Enterprise, :name => 'enteprise template', :identifier => 'enterprise_template') | |
187 | 189 | assert_equal 3, active_template.boxes.size |
188 | 190 | |
189 | 191 | e = Environment.default |
... | ... | @@ -191,7 +193,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
191 | 193 | e.enterprise_template = active_template |
192 | 194 | e.save! |
193 | 195 | |
194 | - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
196 | + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
195 | 197 | |
196 | 198 | p = create_user('test_user').person |
197 | 199 | ent.enable(p) |
... | ... | @@ -201,13 +203,13 @@ class EnterpriseTest < ActiveSupport::TestCase |
201 | 203 | |
202 | 204 | should 'create EnterpriseActivation task when creating with enabled = false' do |
203 | 205 | EnterpriseActivation.delete_all |
204 | - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
206 | + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
205 | 207 | assert_equal [ent], EnterpriseActivation.find(:all).map(&:enterprise) |
206 | 208 | end |
207 | 209 | |
208 | 210 | should 'create EnterpriseActivation with 7-characters codes' do |
209 | 211 | EnterpriseActivation.delete_all |
210 | - Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
212 | + create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false) | |
211 | 213 | assert_equal 7, EnterpriseActivation.find(:first).code.size |
212 | 214 | end |
213 | 215 | |
... | ... | @@ -236,7 +238,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
236 | 238 | should 'list product categories full name' do |
237 | 239 | subcategory = fast_create(ProductCategory, :name => 'Products subcategory', :parent_id => @product_category.id) |
238 | 240 | ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') |
239 | - p = ent.products.create!(:name => 'test prod', :product_category => subcategory) | |
241 | + p = create(Product, :name => 'test prod', :product_category => subcategory, :enterprise => ent) | |
240 | 242 | |
241 | 243 | assert_equal [p.category_full_name], ent.product_categories |
242 | 244 | end |
... | ... | @@ -256,7 +258,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
256 | 258 | end |
257 | 259 | |
258 | 260 | should 'have a default enterprise template' do |
259 | - env = Environment.create!(:name => 'test env') | |
261 | + env = create(Environment, :name => 'test env') | |
260 | 262 | p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => env.id) |
261 | 263 | assert_kind_of Enterprise, p.template |
262 | 264 | end |
... | ... | @@ -270,7 +272,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
270 | 272 | e.inactive_enterprise_template = inactive_template |
271 | 273 | e.save! |
272 | 274 | |
273 | - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :template => another_template, :environment => e) | |
275 | + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :template => another_template, :environment => e) | |
274 | 276 | assert_equal inactive_template, ent.template |
275 | 277 | end |
276 | 278 | |
... | ... | @@ -282,7 +284,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
282 | 284 | should 'return active_enterprise_fields' do |
283 | 285 | e = Environment.default |
284 | 286 | e.expects(:active_enterprise_fields).returns(['contact_phone', 'contact_email']).at_least_once |
285 | - ent = Enterprise.new(:environment => e) | |
287 | + ent = build(Enterprise, :environment => e) | |
286 | 288 | |
287 | 289 | assert_equal e.active_enterprise_fields, ent.active_fields |
288 | 290 | end |
... | ... | @@ -290,7 +292,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
290 | 292 | should 'return required_enterprise_fields' do |
291 | 293 | e = Environment.default |
292 | 294 | e.expects(:required_enterprise_fields).returns(['contact_phone', 'contact_email']).at_least_once |
293 | - enterprise = Enterprise.new(:environment => e) | |
295 | + enterprise = build(Enterprise, :environment => e) | |
294 | 296 | |
295 | 297 | assert_equal e.required_enterprise_fields, enterprise.required_fields |
296 | 298 | end |
... | ... | @@ -298,7 +300,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
298 | 300 | should 'require fields if enterprise needs' do |
299 | 301 | e = Environment.default |
300 | 302 | e.expects(:required_enterprise_fields).returns(['contact_phone']).at_least_once |
301 | - enterprise = Enterprise.new(:environment => e) | |
303 | + enterprise = build(Enterprise, :environment => e) | |
302 | 304 | assert ! enterprise.valid? |
303 | 305 | assert enterprise.errors[:contact_phone.to_s].present? |
304 | 306 | |
... | ... | @@ -308,28 +310,28 @@ class EnterpriseTest < ActiveSupport::TestCase |
308 | 310 | end |
309 | 311 | |
310 | 312 | should 'enable contact' do |
311 | - enterprise = Enterprise.new(:enable_contact_us => false) | |
313 | + enterprise = build(Enterprise, :enable_contact_us => false) | |
312 | 314 | assert !enterprise.enable_contact? |
313 | 315 | enterprise.enable_contact_us = true |
314 | 316 | assert enterprise.enable_contact? |
315 | 317 | end |
316 | 318 | |
317 | 319 | should 'save organization_website with http' do |
318 | - p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') | |
320 | + p = build(Enterprise, :name => 'test_ent', :identifier => 'test_ent') | |
319 | 321 | p.organization_website = 'website.without.http' |
320 | 322 | p.save! |
321 | 323 | assert_equal 'http://website.without.http', p.organization_website |
322 | 324 | end |
323 | 325 | |
324 | 326 | should 'save not add http to empty organization_website' do |
325 | - p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') | |
327 | + p = build(Enterprise, :name => 'test_ent', :identifier => 'test_ent') | |
326 | 328 | p.organization_website = '' |
327 | 329 | p.save! |
328 | 330 | assert_equal '', p.organization_website |
329 | 331 | end |
330 | 332 | |
331 | 333 | should 'save organization_website as typed if has http' do |
332 | - p = Enterprise.new(:name => 'test_ent', :identifier => 'test_ent') | |
334 | + p = build(Enterprise, :name => 'test_ent', :identifier => 'test_ent') | |
333 | 335 | p.organization_website = 'http://website.with.http' |
334 | 336 | p.save |
335 | 337 | assert_equal 'http://website.with.http', p.organization_website |
... | ... | @@ -340,7 +342,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
340 | 342 | e.enable('enterprises_are_disabled_when_created') |
341 | 343 | e.save! |
342 | 344 | |
343 | - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') | |
345 | + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent') | |
344 | 346 | assert_equal false, Enterprise['test_ent'].enabled? |
345 | 347 | end |
346 | 348 | |
... | ... | @@ -349,12 +351,12 @@ class EnterpriseTest < ActiveSupport::TestCase |
349 | 351 | |
350 | 352 | e.enable('enterprises_are_validated_when_created') |
351 | 353 | e.save |
352 | - enterprise = Enterprise.create(:name => 'test enteprise', :identifier => 'test_ent1') | |
354 | + enterprise = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent1') | |
353 | 355 | assert enterprise.validated |
354 | 356 | |
355 | 357 | e.disable('enterprises_are_validated_when_created') |
356 | 358 | e.save |
357 | - enterprise = Enterprise.create(:name => 'test enteprise', :identifier => 'test_ent2') | |
359 | + enterprise = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent2') | |
358 | 360 | assert !enterprise.validated |
359 | 361 | end |
360 | 362 | |
... | ... | @@ -369,7 +371,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
369 | 371 | e.inactive_enterprise_template = inactive_template |
370 | 372 | e.save! |
371 | 373 | |
372 | - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') | |
374 | + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent') | |
373 | 375 | assert_equal 1, ent.boxes.size |
374 | 376 | end |
375 | 377 | |
... | ... | @@ -384,23 +386,23 @@ class EnterpriseTest < ActiveSupport::TestCase |
384 | 386 | e.inactive_enterprise_template = inactive_template |
385 | 387 | e.save! |
386 | 388 | |
387 | - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent') | |
389 | + ent = create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent') | |
388 | 390 | assert_equal 3, ent.boxes.size |
389 | 391 | end |
390 | 392 | |
391 | 393 | should 'collect the highlighted products with image' do |
392 | 394 | env = Environment.default |
393 | 395 | e1 = fast_create(Enterprise) |
394 | - p1 = e1.products.create!(:name => 'test_prod1', :product_category_id => @product_category.id) | |
396 | + p1 = create(Product, :name => 'test_prod1', :product_category_id => @product_category.id, :enterprise => e1) | |
395 | 397 | products = [] |
396 | 398 | 3.times {|n| |
397 | - products.push(Product.create!(:name => "product #{n}", :enterprise_id => e1.id, | |
399 | + products.push(create(Product, :name => "product #{n}", :enterprise_id => e1.id, | |
398 | 400 | :highlighted => true, :product_category_id => @product_category.id, |
399 | 401 | :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } |
400 | 402 | )) |
401 | 403 | } |
402 | - Product.create!(:name => "product 4", :enterprise_id => e1.id, :product_category_id => @product_category.id, :highlighted => true) | |
403 | - Product.create!(:name => "product 5", :enterprise_id => e1.id, :product_category_id => @product_category.id, :image_builder => { | |
404 | + create(Product, :name => "product 4", :enterprise_id => e1.id, :product_category_id => @product_category.id, :highlighted => true) | |
405 | + create(Product, :name => "product 5", :enterprise_id => e1.id, :product_category_id => @product_category.id, :image_builder => { | |
404 | 406 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') |
405 | 407 | }) |
406 | 408 | assert_equal products, e1.highlighted_products_with_image |
... | ... | @@ -409,8 +411,8 @@ class EnterpriseTest < ActiveSupport::TestCase |
409 | 411 | should 'has many inputs through products' do |
410 | 412 | enterprise = fast_create(Enterprise) |
411 | 413 | product = fast_create(Product, :enterprise_id => enterprise.id, :product_category_id => @product_category.id) |
412 | - product.inputs << Input.new(:product_category => @product_category) | |
413 | - product.inputs << Input.new(:product_category => @product_category) | |
414 | + product.inputs << build(Input, :product_category => @product_category) | |
415 | + product.inputs << build(Input, :product_category => @product_category) | |
414 | 416 | |
415 | 417 | assert_equal product.inputs, enterprise.inputs |
416 | 418 | end |
... | ... | @@ -451,7 +453,7 @@ class EnterpriseTest < ActiveSupport::TestCase |
451 | 453 | |
452 | 454 | |
453 | 455 | activity = ActionTracker::Record.last |
454 | - scrap = Scrap.create!(defaults_for_scrap(:sender => person, :receiver => enterprise, :content => 'A scrap')) | |
456 | + scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => enterprise, :content => 'A scrap')) | |
455 | 457 | |
456 | 458 | assert_equal [scrap], enterprise.activities.map { |a| a.klass.constantize.find(a.id) } |
457 | 459 | end | ... | ... |