Commit Graph

43 Commits

Author SHA1 Message Date
Ryuta Kamizono
a206aecedf chore: Use e.g. which is the more used spelling
```
% git grep -i '\be\.g\.' | wc -l
290
```
2021-07-21 09:17:54 +09:00
Alex Ghiculescu
4b42beb3b8 Log a warning when assertions are incorrectly nested and errors are raised
Follow up to https://github.com/rails/rails/pull/37313

- Adds regression tests
- Logs a warning in cases where assertions are nested in a way that's likely to be confusing
2021-07-12 10:37:43 -05:00
George Claghorn
21aea0db55 Add from: option to ActiveSupport::TestCase#assert_no_changes
Permit asserting on the initial value that is expected not to change.
2021-05-23 16:26:52 -04:00
Aaron Patterson
d5ac941ddc
Remove special case filtering for Procs.
I'm writing this patch for two purposes:

1. I want to reduce the number of times `object_id` is called.  Calling
   `object_id` can have negative impacts on performance in Ruby 2.7+, so
   it would be nice to stop calling it.

2. I'm not sure why we're treating lambdas specially here.  It looks
   like we wanted to prevent people from skipping callbacks that were
   defined with a lambda, but I think that is silly.  If the user has a
   reference to a lambda, and they want to skip it, we should let them.

I think this cleans up some code, helps with performance, and is a more
intuitive interface.
2021-03-02 17:20:35 -08:00
Kasper Timm Hansen
51d73fb84b
Merge pull request #37624
Closes #37624
2020-02-17 01:32:23 +01:00
Hrvoje Šimić
074265a6c1
Update assert_changes and assert_no_changes to use assert_equal
This provides better more specific diffs when comparing complex objects.

Reverts bbe437faecca5fd6bdc2327a4bc7a31ba21afe2e.

Closes #37507. Closes #38220.

Co-authored-by: michdsouza <michd2005@gmail.com>
Co-authored-by: Rahul Purohit <rahulpuroht@gmail.com>
Co-authored-by: Nicholas Koh <nkohwl@gmail.com>
2020-02-17 01:30:50 +01:00
Michael Grosser
203998c916
allow running each test with pure ruby path/to/test.rb
also:
 - makes test dependencies obvious
 - makes tests runnable from within subfolders
2019-12-18 08:49:19 -06:00
Ryuta Kamizono
c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
Ryuta Kamizono
ea65d92f19
Enable Lint/UselessAssignment cop to avoid unused variable warnings (#34904)
* Enable `Lint/UselessAssignment` cop to avoid unused variable warnings

Since we've addressed the warning "assigned but unused variable"
frequently.

370537de05092aeea552146b42042833212a1acc
3040446cece8e7a6d9e29219e636e13f180a1e03
5ed618e192e9788094bd92c51255dda1c4fd0eae
76ebafe594fc23abc3764acc7a3758ca473799e5

And also, I've found the unused args in c1b14ad which raises no warnings
by the cop, it shows the value of the cop.
2019-01-09 18:09:01 +09:00
lxxxvi
4baad7bec6 added tests for assert_no_difference with multiple expressions 2018-07-08 15:15:16 +02:00
lxxxvi
f4f7659acf Clearer error message in assert_changes
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.
2018-05-05 14:11:59 +02:00
yuuji.yaginuma
a7491b73f8 Fix test class name for Assertions module
Because this class includes not only `assert_difference` but also tests
of other assertion methods.
2018-04-08 12:14:06 +09:00
Julien Meichelbeck
e0f0d717d6 Support hash as first argument in assert_difference. (#31600)
* Support hash as first argument for `assert_difference`.

This allows to specify multiple numeric differences in the same assertion.
Example:

    assert_difference 'Article.count' => 1, 'Notification.count' => 2 do
      # post :create, params: { article: {...} }
    end

* Support error message when passing a hash as a first parameter

* Format CHANGELOG properly

[Julien Meichelbeck + Rafael Mendonça França]
2018-01-18 15:20:34 -05:00
Daniel Ma
af0361da0a assert_changes should always assert some change
While using `assert_changes`, I came across some unexpected behavior:
if you provide a `to:` argument, and the expression matches but didn't
actually change, the assertion will pass.

The way `assert_changes` reads, I assumed that it would both assert
that there was any change at all, _and_ that the expression changed to
match my `to:` argument.

In the case of just a `from:` argument, `assert_changes` does what I
expect as well. It asserts that the before value `=== from` and that
the after value changed.

My key change is that `assert_changes` will now _always_ assert that
expression changes, no matter what combination of `from:` and `to:`
arguments
2017-11-13 11:49:35 -08:00
Genadi Samokovarov
bbe437faec Use plain assert in assert_changes to avoid MT6 refutes
Seeing the previously issued PRs about it, we can avoid the `nil`
comparisons that can happen in `assert_changes` by using plain `assert`
calls.

This is to avoid a deprecation warning about comparing `nil` values in
`assert_equal` for Minitest 5 and a crash in Minitest 6.

You can see the preparations done in [`assert_equal`][ae]. You can also
see that [`assert`][a] does not care about `nil`s.

[ae]: ca6a71ca90/lib/minitest/assertions.rb (L159-L188)
[a]: ca6a71ca90/lib/minitest/assertions.rb (L131-L142)
2017-11-07 10:42:07 +02:00
Shuhei Kitagawa
03dd47ff21 removed unnecessary semicolons 2017-10-28 17:39:58 +09:00
Akira Matsuda & yui-knk
8ad8093acc Suppress "unused variable" in Ruby 2.5 2017-09-01 18:43:22 +09:00
Koichi ITO
ac717d65a3 [Active Support] rubocop -a --only Layout/EmptyLineAfterMagicComment 2017-07-11 13:12:32 +09:00
Kir Shatrov
72950568dd Use frozen-string-literal in ActiveSupport 2017-07-09 15:08:29 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
yuuji.yaginuma
9b487b939c Remove unused test class
`AlsoDoingNothingTest` was added in cf9be89.
It seems that it added to confirm that the test works in the child class
of `ActiveSupport::TestCase`.
But now basically use `ActiveSupport::TestCase` in test, so I think
it is unnecessary.
2017-05-26 13:23:51 +09:00
Akira Matsuda
3fe6a5d510 Privatize unneededly protected methods in Active Support tests 2016-12-24 00:22:29 +09:00
yuuji.yaginuma
392d03a292 use message that specified in argument to error message 2016-08-31 08:52:23 +09:00
yuuji.yaginuma
6582c7c54a use inspect for show from value
If `from` is nil, in order to avoid the blank is showed.
2016-08-29 09:06:10 +09:00
Xavier Noria
a731125f12 applies new string literal convention in activesupport/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:10:53 +02:00
Genadi Samokovarov
16f24cd10f Introduce assert_changes and assert_no_changes
Those are assertions that I really do miss from the standard
`ActiveSupport::TestCase`. Think of those as a more general version of
`assert_difference` and `assert_no_difference` (those can be implemented
by assert_changes, should this change be accepted).

Why do we need those? They are useful when you want to check a
side-effect of an operation. `assert_difference` do cover a really
common case, but we `assert_changes` gives us more control. Having a
global error flag? You can test it easily with `assert_changes`. In
fact, you can be really specific about the initial state and the
terminal one.

```ruby
error = Error.new(:bad)
assert_changes -> { Error.current }, from: nil, to: error do
  expected_bad_operation
end
```

`assert_changes` follows `assert_difference` and a string can be given
for evaluation as well.

```ruby
error = Error.new(:bad)
assert_changes 'Error.current', from: nil, to: error do
  expected_bad_operation
end
```

Check out the test cases if you wanna see more examples.

🍻
2016-07-17 12:58:57 +03:00
Lucas Mazza
564b162015 Make assert_difference return the result of the yielded block.
With this we can perform new assertions on the returned value without having
to cache it with an outer variable or wrapping all subsequent assertions inside
the `assert_difference` block.

Before:

```
post = nil
assert_difference -> { Post.count }, 1 do
  Post.create
end

assert_predicate post, :persisted?
```

Now:

```
post = assert_difference -> { Post.count } do
  Post.create
end

assert_predicate post, :persisted?
```
2015-09-24 14:17:49 -03:00
Guo Xiang Tan
5a973b314b Remove alias for i_suck_and_my_tests_are_order_dependent. 2015-03-24 00:27:53 +08:00
Rafael Mendonça França
5f777e4b5e Change the default test order from :sorted to :random 2015-01-04 11:58:41 -03:00
Godfrey Chan
2b41343c34 Default to sorting user's test cases for now
Goals:

1. Default to :random for newly generated applications
2. Default to :sorted for existing applications with a warning
3. Only show the warning once
4. Only show the warning if the app actually uses AS::TestCase

Fixes #16769
2014-09-08 05:32:16 -07:00
Zachary Scott
5efcf3800e Move as/test_test to as/test_case_test 2014-08-19 23:40:42 -07:00
kennyj
6c2cbc6fff Remove active_support/testing/pending.rb was deprecated. 2013-06-01 21:23:19 +09:00
Ryan Davis
3073c53198 Updates to make rails 4 happy with minitest 5:
+ Namespace changes, overhaul of runners.
+ Internal ivar name changes
- Removed a logger globally applied to tests that spew everywhere?!?
+ Override Minitest#__run to sort tests by name.
+ Reworked testing isolation to work with the new cleaner architecture.
- Removed a bunch of tests that just test minitest straight up. I think these changes were all merged to minitest 4 a long time ago.
- Minor report output differences.
2013-05-06 17:38:45 -07:00
Carlos Antonio da Silva
da47960893 Fix failures in AS with minitest 4.7.4 2013-05-01 23:21:08 -03:00
Carlos Antonio da Silva
92da512125 Properly deprecate #pending from AS::TestCase
Check https://github.com/rails/rails/pull/4575#issuecomment-5765575.
2012-11-18 21:34:00 -02:00
Rafael Mendonça França
ae6d2efd84 Make the tests pass with minitest 4.2 2012-11-05 15:12:09 -02:00
James Mead
b057fab638 Exceptions like Interrupt should not be rescued.
Neither Test::Unit nor MiniTest rescue exceptions like Interrupt or
NoMemoryError, but ActiveSupport::Testing::SetupAndTeardown#run which
overrides MiniTest::Unit::TestCase#run rescues them.

Rescuing an Interrupt exception is annoying, because it means when you
are running a lot of tests e.g. when running one of the rake test tasks,
you cannot break out using ctrl-C.

Rescuing exceptions like NoMemoryError is foolish, because the most
sensible thing to happen is for the process to terminate as soon as
possible.

This solution probably needs some finessing e.g. I'm not clear whether
the assumption is that only MiniTest is supported. Also early versions
of MiniTest did not have this behaviour. However, hopefully it's a
start.

Integrating with Test::Unit & MiniTest has always been a pain. It would
be great if both of them provided sensible extension points for the kind
of things that both Rails and Mocha want to do.
2012-05-28 22:44:28 +01:00
Aaron Patterson
f65559fcc2 make sure the test case name is nil 2012-01-06 15:17:08 -08:00
Rafael Mendonça França
b2fca9533e No need to check if MiniTest::Assertions is defined 2012-01-06 13:31:39 -03:00
Aaron Patterson
e477fc1147 fixing test case test on 1.9.3dev 2011-03-01 10:14:09 -08:00
Aaron Patterson
b7c49cedba calling correct method on minitest for test name when teardown callback fails 2010-10-04 09:34:34 -07:00
Aaron Patterson
ccf228b027 [#5406 state:resolved] calling the correct method on minitest to obtain the test name 2010-10-04 09:33:35 -07:00