Commit 7225f597649f6079c85e9a4567092f533862d5ee
1 parent
1668fe7d
Exists in
master
and in
29 other branches
Now really avoid division by zero
(ActionItem1682)
Showing
2 changed files
with
10 additions
and
3 deletions
Show diff stats
app/models/profile_list_block.rb
@@ -21,7 +21,7 @@ class ProfileListBlock < Block | @@ -21,7 +21,7 @@ class ProfileListBlock < Block | ||
21 | end | 21 | end |
22 | 22 | ||
23 | def randomizer | 23 | def randomizer |
24 | - @randomizer ||= "(profiles.id % #{rand(profile_count + 1)})" | 24 | + @randomizer ||= "(profiles.id % #{rand(profile_count) + 1})" |
25 | end | 25 | end |
26 | 26 | ||
27 | # the title of the block. Probably will be overriden in subclasses. | 27 | # the title of the block. Probably will be overriden in subclasses. |
test/unit/profile_list_block_test.rb
@@ -153,8 +153,15 @@ class ProfileListBlockTest < Test::Unit::TestCase | @@ -153,8 +153,15 @@ class ProfileListBlockTest < Test::Unit::TestCase | ||
153 | should 'randomize using modulo operator and random number' do | 153 | should 'randomize using modulo operator and random number' do |
154 | block = ProfileListBlock.new | 154 | block = ProfileListBlock.new |
155 | block.expects(:profile_count).returns(10) | 155 | block.expects(:profile_count).returns(10) |
156 | - block.expects(:rand).with(11).returns(5) | ||
157 | - assert_match /profiles.id % 5/, block.randomizer | 156 | + block.expects(:rand).with(10).returns(5) |
157 | + assert_match /profiles.id % 6/, block.randomizer | ||
158 | + end | ||
159 | + | ||
160 | + should 'not divide by zero' do | ||
161 | + block = ProfileListBlock.new | ||
162 | + block.stubs(:profile_count).returns(0) | ||
163 | + block.expects(:rand).returns(0) | ||
164 | + assert_no_match /profiles.id % 0/, block.randomizer | ||
158 | end | 165 | end |
159 | 166 | ||
160 | end | 167 | end |