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

Don't hide underlying test failure in ActiveRecord::TestCase#assert_sql #44397

Closed
wants to merge 1 commit into from

Conversation

drcapulet
Copy link
Contributor

If a test case raises an error that prevents a query from being executed, the ensure block will override that failure which hampers debugging efforts when writing test cases.

@skipkayhil
Copy link
Member

Instead of wrapping in unless $!, couldn't you remove ensure?

@byroot
Copy link
Member

byroot commented Feb 12, 2022

Look like this one was missed in #37313

The best solution would be to use _assert_nothing_raised_or_warn, see: 4b42beb

@drcapulet
Copy link
Contributor Author

Yeah, I can definitely simplify this - just the result of staring at this code too long. I'm a bit confused as to the use of _assert_nothing_raised_or_warn? Or is that just to have the underlying exception warn?

@byroot
Copy link
Member

byroot commented Feb 16, 2022

Or is that just to have the underlying exception warn?

Yes.

@drcapulet
Copy link
Contributor Author

Ah gotcha - the case I was actually originally having issues wasn't necessarily an expectation failure, just a normal exception. Is there a reason the assertions are stuffed into the ensure block and not just:

def assert_sql(*patterns_to_match, &block)
  capture_sql(&block)

  failed_patterns = []

  patterns_to_match.each do |pattern|
     failed_patterns << pattern unless SQLCounter.log_all.any? { |sql| pattern === sql }
  end

  assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{SQLCounter.log.size == 0 ? '' : "\nQueries:\n#{SQLCounter.log.join("\n")}"}"
end

@byroot
Copy link
Member

byroot commented Feb 16, 2022

the case I was actually originally having issues wasn't necessarily an expectation failure

Yes. _assert_nothing_raised_or_warn is not for assertion failures, but for regular app errors.

Is there a reason the assertions are stuffed into the ensure block and not just:

It was likely for the same reason _assert_nothing_raised_or_warn was introduced, just not well done.

byroot added a commit to byroot/rails that referenced this pull request Feb 19, 2022
Fix: rails#44397
Ref: rails#37313
Ref: rails#42459

This avoid mistakes such as:

```ruby
assert_raise Something do
  assert_queries(1) do
    raise Something
  end
end
```

Co-Authored-By: Alex Coomans <alexc@squareup.com>
byroot added a commit that referenced this pull request Feb 19, 2022
Fix: #44397
Ref: #37313
Ref: #42459

This avoid mistakes such as:

```ruby
assert_raise Something do
  assert_queries(1) do
    raise Something
  end
end
```

Co-Authored-By: Alex Coomans <alexc@squareup.com>
@byroot
Copy link
Member

byroot commented Feb 19, 2022

Fixed in #44484. I gave you co-authorship for bringing up the issue.

Thanks!

@drcapulet
Copy link
Contributor Author

@byroot Thanks!

@drcapulet drcapulet deleted the alexc-ar-test-helper branch March 1, 2022 06:40
jyoun-godaddy pushed a commit to jyoun-godaddy/activestorage that referenced this pull request Jul 5, 2022
Fix: rails/rails#44397
Ref: rails/rails#37313
Ref: rails/rails#42459

This avoid mistakes such as:

```ruby
assert_raise Something do
  assert_queries(1) do
    raise Something
  end
end
```

Co-Authored-By: Alex Coomans <alexc@squareup.com>
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.

None yet

3 participants