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

How can I specify Partial Mockup for a Laravel Facade? #242

Closed
newtonianb opened this issue Jan 14, 2014 · 5 comments
Closed

How can I specify Partial Mockup for a Laravel Facade? #242

newtonianb opened this issue Jan 14, 2014 · 5 comments

Comments

@newtonianb
Copy link

First I tried this

$mock = m::mock('Cartalyst\Sentry\Facades\Laravel\Sentry');
$mock->shouldReceive('getUser')->once()->andReturn($userInst);

But it gave me

Fatal error: Cannot redeclare Mockery_1964315998_Cartalyst_Sentry_Facades_Laravel_Sentry::shouldReceive()

So then I found out the Laravel Facades already implement Mockery so I tried directly on the facade.

Sentry::shouldReceive('getUser')->once()->andReturn($userInst);

But now the problem is it's not finding the other functions for that objects essentially I need the behavior of a partial mockup here but I don't see how do tell it.

BadMethodCallException: Method Mockery_2115409749_Cartalyst_Sentry_Sentry::check() does not exist on this mock object

This is what I need

A traditional partial mock defined ahead of time which methods of a class are to be mocked and which are to left unmocked (i.e. callable as normal). The syntax for creating traditional mocks is:

$mock = \Mockery::mock('MyClass[foo,bar]');
In the above example, the foo() and bar() methods of MyClass will be mocked but no other MyClass methods are touched. You will need to define expectations for the foo() and bar() methods to dictate their mocked behaviour.
@modess
Copy link

modess commented Jan 14, 2014

👍 Since the last PR got merged, I've had the exact same problem with Laravel methods not being mocked.

@aik099
Copy link

aik099 commented Jan 14, 2014

With Sentry::shouldReceive('getUser')->once()->andReturn($userInst); code you're not creating a partial mock, but a full one. Please take a look at: https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Facades/Facade.php
This way if you're not setting expectations on every method to be called, then it won't work.

Also Mockery_2115409749_Cartalyst_Sentry_Sentry mocked class name suggests, that you're not using latest dev-master version, because mock naming logic has changed there.

Please try updating to latest dev Mockery version and tell what happened.

@davedevelopment
Copy link
Collaborator

@newtonianb as @aik099 suggested, if you want a partial mock, you need to tell mockery. Runtime mocks are usually suffice and should be easier for you. I'm not 100% sure (I don't use Laravel), but I think once you've called shouldReceive, you should be able to tell the mock to act as a partial:

Sentry::shouldReceive('getUser')->once()->andReturn($userInst);
Sentry::makePartial();

@modess that sounds like a different issue, could you post a new bug with more details, preferably a minimal testcase.

@newtonianb
Copy link
Author

ah great! thank you

@peterkinnaird
Copy link

What a perfectly timed question. I was running into this exact same problem. Your suggestion was spot on. Thanks!

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

5 participants