-
Notifications
You must be signed in to change notification settings - Fork 17
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
new does not return a new object #48
Comments
I see your point, you are expecting the local This is a fair point. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you call
new
twice with the same package name, you get the same object. This makes the entire thing useless.We have a test library full of helper functions that set up common mocks. In order for this to work at all it must hang onto a reference to the mocked object.
If a test then later tries to mock the same module, it receives the same object! Then, it sets up a mock on that object. It is perfectly reasonable to expect that when that object goes away, the mocked behaviour goes away.
It is not reasonable to expect that the object will not go away after all!
The above example causes a test failure. In the example,
My::DB::Schema
uses objects that, whensave
is called on them, send themselves to this example API, by means ofMy::API::Client
.My::API::Client
is also set up to mock GET requests to always return some example default object.Because
Test::MockModule
does not return a new object whennew
is called, the developer who wrote the test mistakenly believes that their subtest will constrain the mockedset_object
to the scope of that subref. However, this mock is permanent.Later in the test,
save
is called on another object, and the test fails, because a mock that should have gone away has not gone away.new
should always return a new object, so that developers can be confident that it will go out of scope when they think it will.The text was updated successfully, but these errors were encountered: