Summer Sale! All accounts are 50% off this week.

MikeFromHL's avatar

Testing A Facade With A Fluent Interface

I'm having trouble figuring out how to assert that a method at the end of a chain on a facade has been called.

I'm using the huddledigital/zendesk-laravel package that provides a Huddle\Zendesk\Facades\Zendesk facade that I'm using in my controller to create a Zendesk ticket. The call looks like Zendesk::tickets()->create($data).

This is the method that is called by the controller when the /api/tickets/new route is posted to:

use Huddle\Zendesk\Facades\Zendesk;

public function create(Request $request)
{
    Zendesk::tickets()->create($request->ticket);
}

This is my test:

use Huddle\Zendesk\Facades\Zendesk;

public function testCreateZendeskTicket()
{
    Zendesk::spy();

    $this->actingAs($this->user)
         ->json('POST', '/api/tickets/new', ['subject' => 'Testing']);

    Zendesk::shouldHaveReceived('tickets->create');
}

I am simply attempting to spy on the facade and assert that Zendesk::tickets()->create() is called by the controller. However, when the test is run, it returns

Method tickets->create(<Any Arguments>) from Mockery_0_Huddle_Zendesk_Services_ZendeskService should be called
 at least 1 times but called 0 times.

What am I doing wrong?

0 likes
8 replies
jlrdw's avatar

Does everything work for real if you create a ticket?

jlrdw's avatar

Just curious then why test, if it really works. Most testing I do is looking for areas to refactor, I am not looking for does "something work". But just my opinion on this.

MikeFromHL's avatar

@jlrdw My goal is to not actually create a ticket every time I run the test. I simply want to assert that the method responsible for creating a ticket was called.

jlrdw's avatar

@michaelferry You said that it works for real, if it works when actually creating a ticket, why do you need the assert that it would? Real World actually working isn't good enough?

This is the thing about some test, I have seen dozens of post where code works but problems in the test.

So would one need to write a test to "test the test". Is this something you actually need? I suggest just test the things like the docs show. If not covered then just do "real World" test. Only suggestions.

Edit:

Try the HTTP testing methods.

MohamedTammam's avatar

I don't know that syntax you wrote.

I think it should be just

Zendesk::shouldHaveReceived('tickets');

To test against the create method you need to test Zendesk package itself.

MohamedTammam's avatar

@michaelferry From that docs

The long chain of method calls isn’t necessarily a bad thing, assuming they each link back to a local object the calling class knows

I assume that create method isn't a part of the facade object.

Again, I'm not sure about that, I didn't use that lib before.

Please or to participate in this conversation.