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 352<br />

.<br />

You can find a boring description at: http://en.wikipedia.org/wiki/Functional_testing.<br />

http://en.wikipedia.org/wiki/Functional_testing<br />

Acceptance Tests<br />

Acceptance tests are test which look at a much broader range of functionality. These are often called<br />

end-to-end tests because they are concerned with the correct functionality over a range of functions,<br />

methods classes etc.<br />

.<br />

You can find a boring description at: http://en.wikipedia.org/wiki/Acceptance_testing.<br />

http://en.wikipedia.org/wiki/Acceptance_testing<br />

Am I Writing Unit Or Functional Tests?<br />

When it comes to writing tests in Laravel 4, developers often think they are writing unit tests when<br />

they are actually writing functional tests. The difference is small but significant.<br />

Laravel provides a set of test classes (a base TestCase class, and an ExampleTest class). If you<br />

base your tests off of these, you are probably writing functional tests. The TestCase class actually<br />

initialises the Laravel 4 framework. If your code depends on that being the case (accessing the aliases,<br />

service providers etc.) then your tests aren’t isolated. They have dependencies and they need to be<br />

run in order.<br />

When your tests only require the underlying PHPUnit or PHPSpec classes, then you may be writing<br />

unit tests.<br />

How does this affect us? Well - if your goal is to write functional tests, and you have decent test<br />

coverage then you’re doing ok. But if you want to write true unit tests, then you need to pay attention<br />

to how your code is constructed. If you’re using the handy aliases, which Laravel provides, then<br />

writing unit tests may be tough.<br />

That should be enough theory to get us started. Let’s take a look at some code…<br />

Fat Controllers<br />

It’s not uncommon to find controllers with actions resembling the following:

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

Saved successfully!

Ooh no, something went wrong!