[noosfero/noosfero][master] HighlightsBlock: fix test for randomness

Antonio Terceiro gitlab at gitlab.com
Fri Apr 24 12:30:25 BRT 2015


Antonio Terceiro pushed to branch master at Noosfero / noosfero


Commits:
cb62ddad by Antonio Terceiro at 2015-04-24T11:35:26Z
HighlightsBlock: fix test for randomness

Obviously a test for randomness that is not well designed will fail ...
randomly!

- - - - -


2 changed files:

- app/models/highlights_block.rb
- test/unit/highlights_block_test.rb


Changes:

=====================================
app/models/highlights_block.rb
=====================================
--- a/app/models/highlights_block.rb
+++ b/app/models/highlights_block.rb
@@ -26,8 +26,16 @@ class HighlightsBlock < Block
   end
 
   def featured_images
-    block_images = images.select{|i| !i[:image_src].nil? }.sort { |x, y| x[:position] <=> y[:position] }
-    shuffle ? block_images.shuffle : block_images
+    images = get_images
+    shuffle ? images.shuffle : images
+  end
+
+  def get_images
+    images.select do |i|
+      !i[:image_src].nil?
+    end.sort do |x, y|
+      x[:position] <=> y[:position]
+    end
   end
 
   def content(args={})


=====================================
test/unit/highlights_block_test.rb
=====================================
--- a/test/unit/highlights_block_test.rb
+++ b/test/unit/highlights_block_test.rb
@@ -109,33 +109,14 @@ class HighlightsBlockTest < ActiveSupport::TestCase
   end
 
   should 'list images randomically' do
-    f1 = mock()
-    f1.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(1).returns(f1)
-    f2 = mock()
-    f2.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(2).returns(f2)
-    f3 = mock()
-    f3.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(3).returns(f3)
-    f4 = mock()
-    f4.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(4).returns(f4)
-    f5 = mock()
-    f5.expects(:public_filename).returns('address')
-    UploadedFile.expects(:find).with(5).returns(f5)
     block = HighlightsBlock.new
-    i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'}
-    i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'}
-    i3 = {:image_id => 3, :address => '/address', :position => 2, :title => 'address'}
-    i4 = {:image_id => 4, :address => '/address', :position => 5, :title => 'address'}
-    i5 = {:image_id => 5, :address => '/address', :position => 4, :title => 'address'}
-    block.images = [i1,i2,i3,i4,i5]
     block.shuffle = true
-    block.save!
-    block.reload
-    assert_equal [i1,i2,i3,i4,i5], block.images
-    assert_not_equal [i2,i3,i1,i4,i5], block.featured_images
+
+    images = []
+    block.expects(:get_images).returns(images)
+    images.expects(:shuffle).returns(images)
+
+    block.featured_images
   end
 
   [Environment, Profile].each do |klass|



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/cb62ddad16c16258029645b2c76fb413e07f0c19
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150424/4a61c991/attachment.html>


More information about the Noosfero-dev mailing list