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

Fix unstubbing of not defined methods #811

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

GermanDZ
Copy link

@GermanDZ GermanDZ commented Oct 23, 2019

After stubbing a method not really defined, but the module (probably will behave the same for classes) accepts a message (respond to...) and it's managed by meta-prorgramming used "method_missing" then the unstubbing is not working because the stubbed method is still there.

This fix the behaviour, ensuring after the stubbing block the stubbed method is removed and calls to "respond_to..." are called as usual.

@GermanDZ GermanDZ force-pushed the fix-stubbing-not-really-defined-methods branch 2 times, most recently from d458a18 to e02edf4 Compare October 24, 2019 08:49
@GermanDZ GermanDZ changed the title [WIP] Fix unstubbing of not defined methods Fix unstubbing of not defined methods Oct 31, 2019
@GermanDZ GermanDZ force-pushed the fix-stubbing-not-really-defined-methods branch from e02edf4 to ea8edfe Compare May 13, 2021 13:08
@GermanDZ
Copy link
Author

Rebased to the latest master from upstream.

@zenspider zenspider removed their request for review November 15, 2021 19:34
@alekseyl
Copy link

Technically your case is a subset of the #891

i.e. the method should be kept on the singleton_class only if it was defined on the singleton_class already. Otherwise it should be removed after stubbing is done.

You can examine your added test cases against my branch.

@GermanDZ
Copy link
Author

Great! my tests are passing with your fix. 👏

I will close this PR once your PR is merged.

@alekseyl
Copy link

Cool, but we still should do something with the tests you wrote for dynamic module definitions. May be re-PR with tests only.

@GermanDZ
Copy link
Author

👍 I will rebase this branch after your merge and use my tests to protect the code against regressions.

@luke-gru
Copy link

luke-gru commented Apr 3, 2023

I think this solution is good. Question though: why, in the test, are you manually setting @assertion_count? Also, can the maintainers please take a look at this issue?

@GermanDZ
Copy link
Author

GermanDZ commented Apr 3, 2023

Question though: why, in the test, are you manually setting @assertion_count?

Probably just left-overs from previous code, tomorrow I'll rebase/re-run the code.

@GermanDZ GermanDZ force-pushed the fix-stubbing-not-really-defined-methods branch from ea8edfe to e3e3a3a Compare April 5, 2023 06:17
@GermanDZ
Copy link
Author

GermanDZ commented Apr 5, 2023

Rebased and removed the fix (only added the tests), still failing.

Ruby 2.7

➜  minitest git:(fix-stubbing-not-really-defined-methods) rake test
Run options: --seed 47573

# Running:

..............................................................S............................................................................S...S......S.....S..F....S..S.SSSS..................................................S..............................S...............S...................S.........................................S...........S.......................................................................................

Finished in 0.232336s, 1859.3761 runs/s, 5375.8350 assertions/s.

  1) Failure:
TestMinitestStub#test_dynamic_method_on_modules [/Users/germandz/Development/minitest/test/minitest/test_minitest_mock.rb:761]:
Expected: false
  Actual: true

432 runs, 1249 assertions, 1 failures, 0 errors, 17 skips

You have skipped tests. Run with --verbose for details.
rake aborted!
Command failed with status (1): [/Users/germandz/.rubies/ruby-2.7.1/bin/rub...]
/Users/germandz/Development/minitest/lib/minitest/test_task.rb:164:in `block in define'
/Users/germandz/.gem/ruby/2.7.1/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
Tasks: TOP => test
(See full trace by running task with --trace)

Ruby 3.2

➜  minitest git:(fix-stubbing-not-really-defined-methods) rake test
Run options: --seed 29173

# Running:

.........................................................................................................................................................................................................................................................................................................................................................................S...S......S.....S..F....S..S.SSSS.....................................

Finished in 0.238538s, 1811.0322 runs/s, 5303.1383 assertions/s.

  1) Failure:
TestMinitestStub#test_dynamic_method_on_modules [/Users/germandz/Development/minitest/test/minitest/test_minitest_mock.rb:761]:
Expected: false
  Actual: true

432 runs, 1265 assertions, 1 failures, 0 errors, 10 skips

You have skipped tests. Run with --verbose for details.
rake aborted!
Command failed with status (1): [/Users/germandz/.rubies/ruby-3.2.2/bin/rub...]
/Users/germandz/Development/minitest/lib/minitest/test_task.rb:164:in `block in define'
Tasks: TOP => test
(See full trace by running task with --trace)

@GermanDZ
Copy link
Author

GermanDZ commented Apr 5, 2023

I think this solution is good. Question though: why, in the test, are you manually setting @assertion_count? Also, can the maintainers please take a look at this issue?

I reviewed the code: @assertion_count is asserted in teardown to ensure all the expected assertions were run.

@luke-gru
Copy link

luke-gru commented Apr 5, 2023

It seems the force-push got rid of your changes to the stub implementation and only kept the tests.

Also can you change the test name to be more explanatory, something like: test_respond_to_is_false_after_stub_nonexisting_method

@GermanDZ
Copy link
Author

It seems the force-push got rid of your changes to the stub implementation and only kept the tests.

Yes, I've rebased to latest version from upstream (my branch was really old). I wanted to be sure the error is still there.

If #891 is fixing the problems I want to know if my test cases are covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants