03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Controller Testing<br />

Testing is a hot topic these days. Everyone knows that having tests is a good thing, but often they’re<br />

either “too busy” to write them, or they just don’t know how.<br />

Even if you do write tests, it may not always be clear what the best way is to prepare for them.<br />

Perhaps you’re used to writing them against a particular framework (which I’m assuming is not<br />

Laravel 4) or you tend to write fewer when you can’t figure out how exactly to test a particular<br />

section of your application.<br />

I have recently spent much time writing tests, and learning spades about how to write them. I have<br />

Jeffrey Way to thank, both for Laravel: Testing Decoded¹ and Laracasts.com². These resources<br />

have taught me just about everything I know about unit/functional testing.<br />

This article is about some of the practical things you can do to make your code easier to test, and<br />

how you would go about writing functional/unit tests against that code. If you take nothing else<br />

away, after reading this, you should be subscribed to Laracasts.com and you should read Laravel:<br />

Testing Decoded.<br />

Since there’s already so much in the way of testing, I thought it would be better just to focus on the<br />

subject of how to write testable controllers, and then how to test that they are doing what they are<br />

supposed to be doing.<br />

.<br />

The code for this chapter can be found at: https://github.com/formativ/tutorial-laravel-4-<br />

testing<br />

https://github.com/formativ/tutorial-laravel-4-testing<br />

Installing Dependencies<br />

For this chapter, we’re going to be using PHPUnit and Mockery. Both of these libraries are used in<br />

testing, and can be installed at the same time, by running the following commands:<br />

¹https://leanpub.com/laravel-testing-decoded<br />

²http://laracasts.com

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

Saved successfully!

Ooh no, something went wrong!