Skip to content

Commit

Permalink
Merge pull request #31011 from danielma/dma/assert-changes-with-to-sh…
Browse files Browse the repository at this point in the history
…ould-still-assert-change

`assert_changes` should always assert some change
  • Loading branch information
kamipo committed Jan 4, 2018
2 parents 652258e + af0361d commit cb86b95
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion activesupport/CHANGELOG.md
@@ -1,10 +1,16 @@
* `assert_changes` will always assert that the expression changes,
regardless of `from:` and `to:` argument combinations.

*Daniel Ma*

* Allow the hash function used to generate non-sensitive digests, such as the
ETag header, to be specified with `config.active_support.hash_digest_class`.

The object provided must respond to `#hexdigest`, e.g. `Digest::SHA1`.

*Dmitri Dolguikh*



## Rails 5.2.0.beta2 (November 28, 2017) ##

* No changes.
Expand Down
11 changes: 6 additions & 5 deletions activesupport/lib/active_support/testing/assertions.rb
Expand Up @@ -156,11 +156,12 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b

after = exp.call

if to == UNTRACKED
error = "#{expression.inspect} didn't change"
error = "#{message}.\n#{error}" if message
assert before != after, error
else
error = "#{expression.inspect} didn't change"
error = "#{error}. It was already #{to}" if before == to
error = "#{message}.\n#{error}" if message
assert before != after, error

unless to == UNTRACKED
error = "#{expression.inspect} didn't change to #{to}"
error = "#{message}.\n#{error}" if message
assert to === after, error
Expand Down
11 changes: 11 additions & 0 deletions activesupport/test/test_case_test.rb
Expand Up @@ -156,6 +156,16 @@ def test_assert_changes_with_to_option
end
end

def test_assert_changes_with_to_option_but_no_change_has_special_message
error = assert_raises Minitest::Assertion do
assert_changes "@object.num", to: 0 do
# no changes
end
end

assert_equal "\"@object.num\" didn't change. It was already 0", error.message
end

def test_assert_changes_with_wrong_to_option
assert_raises Minitest::Assertion do
assert_changes "@object.num", to: 2 do
Expand Down Expand Up @@ -218,6 +228,7 @@ def test_assert_changes_with_to_and_from_and_case_operator
def test_assert_changes_with_message
error = assert_raises Minitest::Assertion do
assert_changes "@object.num", "@object.num should 1", to: 1 do
@object.decrement
end
end

Expand Down

0 comments on commit cb86b95

Please sign in to comment.