When I'm finally ready, I say n, and then I can do any business that I want to take care of after the example. So you can see I can do whatever business I want to do before I do that. That's so that then we can call n in the third line of code there. We have around example, and then you'll notice that we have example declared as a block variable after that. Together, before hooks and after hooks are going to address most of what you're going to need, but there are some rare occasions when you're going to need to use around, and around works a little bit differently. After your example is done, it runs any after hooks that you have in the reverse order, after example, after context, after suite. The before suite runs first, then before context, and then example, and then your example runs. Now if you happen to have all three of these in place, then they run in the order shown. So they tried to clarify it by using context and example, and I think those are superior, so that's what we're going to be using, instead of the older all and each forms. In previous versions of RSpec, these were labeled suite, all, and each, and all and each are still aliases for context and example, but all and each became a little bit confusing. The before example code, then the example, the before example code, then the example, and so on. If you have 10 examples in an example group, then the before example code will run 10 times. This runs the block of code once before each example. In the third example, we are using the example scope. So before it runs anything inside that context, it'll run that block of code. And you'll remember, an example group is defined by describe or context. This runs the block of code once before any of the examples in an example group are executed. In the second example, we are using the context scope. Mostly it's used to do any setup that our entire test suite needs right at the start. This ensures that whether you're running one test or all of tests, this code still runs one time at the very start. Typically, you would put this in your spec helper file inside the RSpec config block, and that's why I've begun it with the config. In the first example, suite indicates the block of code should be run once before the suite of tests. Let's look at before hooks as an example, so we can understand scope. Each of these RSpec methods is going to take an argument, which is a symbol indicating the scope in which it should be run. Write code one time and then tell RSpec to run that code either before, after, or around our examples. It keeps your code cleaner, clearer, and reduces the opportunities for mistakes. It's a good programming practice that says, whenever possible, try to write code one time and then refer to that code whenever you need it again, instead of writing it all over. In programming, there's a fundamental principle called, Don't Repeat Yourself or DRY for short. In this movie, we'll learn how to use before, after, and around hooks to make your tests more efficient.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |