![]() user under test has basic subscription: false.user under test has premium subscription and basic subscription: true.user under test has premium subscription: true.user under test has no subscriptions: false.We test these integrations with all of the scenarios we can think of because they’re faster to run (less setup). (Note that we’re not fully testing an end point). We now move to integration tests, so we can boot up the application and use factories to generate data. ![]() Now, our gate is doing some sort of query against the database to determine if this user has the premium subscription. (In fact, proper unit testing actually helps you write cleaner code - because clean code is required for simpler, faster unit tests.) Let’s take a look at an example where our default Laravel user model has a first and last name, and we want to get a display name - which is first name and last initial.Ĭlass User extends Authenticatable ) Preferably you’re writing your methods simple anyway, so this is easy. It’s meant to convey that these are the simplest forms of testing.Ī way to look at this is that you should be only testing the smallest part of code, or the thing that makes roughly, at max, one decision. Laravel’s default ExampleTest in the tests/Unit directory doesn’t even load the entire application kernel. A default Laravel project even provides a folder and example test for these. Think the size of one method on a class - or a simple function. You write a test that takes the smallest part of a piece of code and tests the outcome from possible inputs. Unit TestĪ unit test is the simplest form of test. So, let’s break down the types that I use in my Laravel project. The idea is that we write code to automatically test other code. Really, a more accurate way to refer to them would be automated tests. There are even more (smoke test, e2e test, dusk tests, etc…) but we’re going to focus on these because they fit nicely in our Laravel ecosystem.Īll of these things are broadly called unit tests. We’re going to focus on four terms: unit test, integration test, feature test and external test. What’s the difference between unit test, integration test, and a feature test? Which should you use for what type of test? Is it just preference or does performance factor in when using Laravel? Let me give you my take from over a decade of unit testing experience.įirst of all, let’s talk about the definitions of these terms.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |