Commit cb62ddad16c16258029645b2c76fb413e07f0c19
1 parent
200ff2b0
Exists in
master
and in
18 other branches
HighlightsBlock: fix test for randomness
Obviously a test for randomness that is not well designed will fail ... randomly!
Showing
2 changed files
with
16 additions
and
27 deletions
Show diff stats
app/models/highlights_block.rb
| @@ -26,8 +26,16 @@ class HighlightsBlock < Block | @@ -26,8 +26,16 @@ class HighlightsBlock < Block | ||
| 26 | end | 26 | end |
| 27 | 27 | ||
| 28 | def featured_images | 28 | def featured_images |
| 29 | - block_images = images.select{|i| !i[:image_src].nil? }.sort { |x, y| x[:position] <=> y[:position] } | ||
| 30 | - shuffle ? block_images.shuffle : block_images | 29 | + images = get_images |
| 30 | + shuffle ? images.shuffle : images | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + def get_images | ||
| 34 | + images.select do |i| | ||
| 35 | + !i[:image_src].nil? | ||
| 36 | + end.sort do |x, y| | ||
| 37 | + x[:position] <=> y[:position] | ||
| 38 | + end | ||
| 31 | end | 39 | end |
| 32 | 40 | ||
| 33 | def content(args={}) | 41 | def content(args={}) |
test/unit/highlights_block_test.rb
| @@ -109,33 +109,14 @@ class HighlightsBlockTest < ActiveSupport::TestCase | @@ -109,33 +109,14 @@ class HighlightsBlockTest < ActiveSupport::TestCase | ||
| 109 | end | 109 | end |
| 110 | 110 | ||
| 111 | should 'list images randomically' do | 111 | should 'list images randomically' do |
| 112 | - f1 = mock() | ||
| 113 | - f1.expects(:public_filename).returns('address') | ||
| 114 | - UploadedFile.expects(:find).with(1).returns(f1) | ||
| 115 | - f2 = mock() | ||
| 116 | - f2.expects(:public_filename).returns('address') | ||
| 117 | - UploadedFile.expects(:find).with(2).returns(f2) | ||
| 118 | - f3 = mock() | ||
| 119 | - f3.expects(:public_filename).returns('address') | ||
| 120 | - UploadedFile.expects(:find).with(3).returns(f3) | ||
| 121 | - f4 = mock() | ||
| 122 | - f4.expects(:public_filename).returns('address') | ||
| 123 | - UploadedFile.expects(:find).with(4).returns(f4) | ||
| 124 | - f5 = mock() | ||
| 125 | - f5.expects(:public_filename).returns('address') | ||
| 126 | - UploadedFile.expects(:find).with(5).returns(f5) | ||
| 127 | block = HighlightsBlock.new | 112 | block = HighlightsBlock.new |
| 128 | - i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'} | ||
| 129 | - i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'} | ||
| 130 | - i3 = {:image_id => 3, :address => '/address', :position => 2, :title => 'address'} | ||
| 131 | - i4 = {:image_id => 4, :address => '/address', :position => 5, :title => 'address'} | ||
| 132 | - i5 = {:image_id => 5, :address => '/address', :position => 4, :title => 'address'} | ||
| 133 | - block.images = [i1,i2,i3,i4,i5] | ||
| 134 | block.shuffle = true | 113 | block.shuffle = true |
| 135 | - block.save! | ||
| 136 | - block.reload | ||
| 137 | - assert_equal [i1,i2,i3,i4,i5], block.images | ||
| 138 | - assert_not_equal [i2,i3,i1,i4,i5], block.featured_images | 114 | + |
| 115 | + images = [] | ||
| 116 | + block.expects(:get_images).returns(images) | ||
| 117 | + images.expects(:shuffle).returns(images) | ||
| 118 | + | ||
| 119 | + block.featured_images | ||
| 139 | end | 120 | end |
| 140 | 121 | ||
| 141 | [Environment, Profile].each do |klass| | 122 | [Environment, Profile].each do |klass| |