Skip to content

Do not leak TestUnit specific methods after Rails 4 #1512

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

Merged
merged 1 commit into from
Jan 15, 2016
Merged

Do not leak TestUnit specific methods after Rails 4 #1512

merged 1 commit into from
Jan 15, 2016

Conversation

ferdynton
Copy link
Contributor

TestUnit requires the build_message method to be defined for its assertions to work properly. After Rails 4, Minitest is used instead of TestUnit so the method should not be included anymore. This is problematic because when using RSpec configured with

config.expect_with :test_unit

the TestUnit assertions try to call build_message on the MinitestAssertionAdapter which will crash.

methods + [:build_message]
# Starting on Rails 4, Minitest is the default testing framework so no
# need to add TestUnit specific methods.
if ::Rails::VERSION::STRING >= '4.0.0'
Copy link
Member

Choose a reason for hiding this comment

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

As a point of style, it's nice to write method behavior that depends on constants by putting the conditional statement outside of the method definition so that it doesn't need to get calculated every time the method is called.

if ::Rails::VERSION::STRING >= '4.0.0'
  def assertion_method_names
    []
  end
else
  def assertion_method_names
    []
  end
end

In this case, define_assertion_delegators only gets called when the concern is included, which is just twice AFAICT, so not a big deal, but wanted to mention it since it's a common pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for mentioning, I've updated the code accordingly

@soulcutter
Copy link
Member

928eddb doesn't seem strictly necessary. RSpec 4.0 is removing 1.8.7 support, so adding changes specifically to address 1.8.7 compatibility in tests that were already working in all rubies is likely to get reverted or backed out soon. In other words, that commit is probably creating some additional work in the near-future.

* TestUnit requires the build_message method to be defined for its
  assertions to work properly. After Rails 4, Minitest is used instead
  of TestUnit so the method should not be included anymore. This is
  problematic because when using RSpec configured with

      config.expect_with :test_unit

  the TestUnit assertions try to call build_message on the
  MinitestAssertionAdapter which will crash.
@ferdynton
Copy link
Contributor Author

@soulcutter It's not strictly necessary, although it improves the quality of the current tests. I removed it, although I still use the same principle on the commit left. Otherwise the build breaks for Ruby <= 1.8.7.

In the end, since the bugfix is really only worth for Rails 4 and above, Ruby 1.8.7 does not matter. You tell me what to do, I'm fine either way. Thanks for reviewing!

@ferdynton
Copy link
Contributor Author

@soulcutter any words on the actual bugfix? Does it make sense?

@soulcutter
Copy link
Member

Yeah, this looks good to me. Sorry for letting it dangle here for a bit, thanks for gently reminding me to revisit it.

soulcutter added a commit that referenced this pull request Jan 15, 2016
…fter_rails_4

Do not leak TestUnit specific methods after Rails 4
@soulcutter soulcutter merged commit 4445423 into rspec:master Jan 15, 2016
soulcutter added a commit that referenced this pull request Jan 15, 2016
sebjacobs pushed a commit to futurelearn/rspec-rails that referenced this pull request Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants