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

Bug: mock single_service #4515

Closed
gmeister2 opened this issue Apr 4, 2021 · 4 comments
Closed

Bug: mock single_service #4515

gmeister2 opened this issue Apr 4, 2021 · 4 comments

Comments

@gmeister2
Copy link

Describe the bug
Cannot mock single_service()

CodeIgniter 4 version
CI 4.1.1

Affected module(s)
system/Common.php -> single_service()
system/Config/BaseService.php -> serviceExists()

Expected behavior, and steps to reproduce if appropriate
When getting a service like this (shared instance):
$example = service('example');
Afterwards you can a mock this when testing with:
Services::injectMock('example', $example);

When getting a service like this (new instance):
$example = single_service('example');
Creating a mock when testing is not possible with:
Services::injectMock('example', $example);

@gmeister2 gmeister2 added the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 4, 2021
@MGatner
Copy link
Member

MGatner commented Apr 4, 2021

Mocked services only apply to shared instances, whereas single_service() is explicitly for acquiring non-shared instances:

Always returns a new instance of the class.

I'm not saying this is the most desirable behavior but whoever created Services built it this way, it is not a bug.

@paulbalandan paulbalandan removed the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 4, 2021
@gmeister2
Copy link
Author

gmeister2 commented Apr 4, 2021

Ok, thanks for your answer. I was unsure whether to label this as a bug or a feature request. My confusion stems from the documentation under Testing / Getting Started / Mocking Services, my understanding was that mocking would apply to all Services.

@MGatner
Copy link
Member

MGatner commented Apr 4, 2021

I agree that mocking being for "shared only" is unintuitive and I would support such a feature request. If you wish to pursue this please post it on the forums (you can link it here) to gather feedback. I am curious whether the team would consider this a breaking change given that some developers may be using mocking with the understanding that it won't affect their new instances?

@gmeister2
Copy link
Author

gmeister2 commented Apr 6, 2021

Link to forum:
http://forum.codeigniter.com/thread-78985.html

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

No branches or pull requests

3 participants