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

[5.7] Easily mock and spy object instances in the container #26171

Merged
merged 1 commit into from
Oct 19, 2018
Merged

[5.7] Easily mock and spy object instances in the container #26171

merged 1 commit into from
Oct 19, 2018

Conversation

TitasGailius
Copy link
Contributor

This adds a simple way to mock and spy object instances in the container.

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->mock(Mailer::class, function ($mock) {
            $mock->shouldReceive('send')->once();
        });


        $this->spy(Dispatcher::class, function ($mock) {
            $mock->shouldReceive('dispatch')->andReturn(false);
        });
    }
}

Without this change you have to do something like this:

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->instance(Mailer::class, Mockery::mock(Mailer::class, function ($mock) {
            $mock->shouldReceive('send')->once();
        }));
    }
}

obviously this is not a big deal but it cleans up code a bit. :)

@mfn
Copy link
Contributor

mfn commented Oct 18, 2018

obviously this is not a big deal but it cleans up code a bit. :)

I like this. As you said, not a big deal but I've my share of code of this nature, duplicating the *::class identifier, etc. Never though of adding a helper, smart.

May simply copy it to my project if it doesn't get merge 😄

@deleugpn
Copy link
Contributor

Simple and really powerful.

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

5 participants