03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Controller Testing 368<br />

1 Mockery::on(function($callback) {<br />

2 $emailMock = Mockery::mock("stdClass");<br />

3<br />

4 $emailMock<br />

5 ->shouldReceive("to")<br />

6 ->atLeast()<br />

7 ->once()<br />

8 ->with("foo");<br />

9<br />

10 $callback($emailMock);<br />

11<br />

12 return true;<br />

13 });<br />

We set up a mock, which expects calls to it’s own methods, and then the original callback is run<br />

with the provided mock. Don’t forget to add return true - that tells mockery that it’s ok to run the<br />

callback with the mock you’ve set up.<br />

At the end of all of this; we’re just testing that methods were called in the correct way. The test<br />

doesn’t worry about making sure the Laravel Mailer class actually sends the mail correctly - that<br />

has it’s own tests.<br />

The repository class is slightly simpler to test:<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!