Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
fix minitest assertions to accept a string as a proc
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Aug 30, 2016
1 parent 2c5d288 commit e6f0527
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ my_hash.compact!
### Minitest Method
```ruby
assert_changed -> { user.name } do
assert_changed 'user.name' do
user.name = "Bob"
end
assert_not_changed -> { user.name } do
user.update(user_params)
end
Expand All @@ -221,7 +225,7 @@ If you want to contribute here are a couple of things you could do:
- Add Tests for Rails methods
- Get the `natural_sort` method to accept a block
- Get the `assert_changed` and `assert_not_changed` method to accept a String with the variable name similar to Rails `assert_difference`
- Get the `assert_changed` and `assert_not_changed` method to accept a String with the variable name similar to Rails ``
# Credits
Expand Down
23 changes: 13 additions & 10 deletions lib/rearmed/monkey_patches/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@

if enabled || Rearmed.dig(Rearmed.enabled_patches, :minitest, :assert_changed)
def assert_changed(expression, &block)
#unless expression.respond_to?(:call)
# expression = lambda{ eval(expression, block.binding) }
# expression = lambda{ block.binding.eval("#{expression}") }
#end
old = expression.call
if expression.respond_to?(:call)
e = expression
else
e = lambda{ block.binding.eval(expression) }
end
old = e.call
block.call
refute_equal old, expression.call
refute_equal old, e.call
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :minitest, :assert_not_changed)
def assert_not_changed(expression, &block)
unless expression.respond_to?(:call)
expression = lambda{ eval(expression, block.binding) }
if expression.respond_to?(:call)
e = expression
else
e = lambda{ block.binding.eval(expression) }
end
old = expression.call
old = e.call
block.call
assert_equal old, expression.call
assert_equal old, e.call
end
end

Expand Down
16 changes: 8 additions & 8 deletions test/tc_rearmed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def test_object
end

def test_minitest
#str = 'first'
#assert_changed "str" do
# str = 'second'
#end
str = 'first'
assert_changed "str" do
str = 'second'
end

str = 'first'
assert_changed ->{ str } do
Expand All @@ -191,10 +191,10 @@ def test_minitest
name = 'second'
end

#name = 'first'
#assert_not_changed 'name' do
# name = 'first'
#end
name = 'first'
assert_not_changed 'name' do
name = 'first'
end

name = 'first'
assert_not_changed ->{ name } do
Expand Down

0 comments on commit e6f0527

Please sign in to comment.