Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearer error message in assert_changes #32822

Merged
merged 1 commit into from May 22, 2018
Merged

Clearer error message in assert_changes #32822

merged 1 commit into from May 22, 2018

Conversation

lxxxvi
Copy link
Contributor

@lxxxvi lxxxvi commented May 5, 2018

If the to: argument is passed to assert_changes and the comparison with the after value fails, the error message only revealed the "actual" value, not the "expected" value.

Example:

test 'after value correct' do
  value = 'x'
  assert_changes 'value', from: 'x', to: 'y' do
    value = 'z'
  end
end

Before the error message said:

"value" didn't change to y

Now it says:

"value" didn't change to as expected
Expected: "y"
  Actual: "z"

When `to:` is passed to `assert_changes`, it now prints the well-known `"Expected: x\n Actual: y"` message. 
Before, the message only contained the actual value.
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

Copy link
Member

@sikachu sikachu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change looks fine. However, I wonder, is this the first time we introduce multi-line assertion failure message (apart from when you set the assertion message yourself)?

@lxxxvi
Copy link
Contributor Author

lxxxvi commented May 6, 2018

@sikachu Thanks for asking this question.

The multi-line assertion failure message would normally come from minitest's assert_equal (or other assertions).

In assert_changes the assertion method assert is used which does not return a multi-line failure message.

So I tested, if assert_equal could be used instead

# Option without manually creating the multi-line message,
# `assert_equal` creates it instead
unless to == UNTRACKED
  error = "#{expression.inspect} didn't change to as expected"
  error = "#{message}.\n#{error}" if message

  assert_equal to, after, error
end

This works. It also returns a multi-line failure:

@object.num should 1.
"@object.num" didn't change to as expected.
Expected: 1
  Actual: -1

But it breaks if to: is a Regexp, because assert_equal does not cover regular expressions.

In order to workaround this, I could come up with this

# Option that also considers if `to:` is a regular expression
unless to == UNTRACKED
  error = "#{expression.inspect} didn't change to as expected"
  error = "#{message}.\n#{error}" if message

  assert_match to, after, error if to.class == Regexp
  assert_equal to, after, error
end

Could be that there's even a better approach...?

@rafaelfranca rafaelfranca merged commit be43b1f into rails:master May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants