Nested Logic: Hooks - RSpec Tutorial

preview_player
Показать описание
Nested Logic: Hooks is a free tutorial by Boris Paskhaver from RSpec course
Link to this course(Special Discount):
This is the best RSpec Course
Course summary:
Utilize test-driven development principles to design and implement clean test specs in Ruby
Master the syntax and structure of RSpec, the most popular Ruby Gem for testing
Reduce dependencies in your test suite by mocking objects with class and instance doubles
Explore the wide collection of RSpec matchers available to test your code
English [Auto]
In this lesson we'll take a look at how we can use our spec hooks within a nested describe or context block. So let's dive right into it. I'll begin with a very basic example in this high level describe. I'm going to add to before hooks one for the context and one for the example somewhere right before context here. Let's give that a do end. BLOCK And in here I'm just going to put puts before context I'm going to copy this code pasted below. We're gonna make this before example I'm going to output before example here and I'm going to do a simple example here it does basic math. I just need some test right here to run and I'm going to expect that one plus one should be equal to two. They should be pretty familiar by now. If I run this back with our spec spec nested hooks we're gonna see at the very top we have before context appear first. That is because this thing runs once before everything else within the block and the block again is this big do any block that we pass to the to the described method. Afterwards we move to before example which only runs once all runs once before. Every example in our current describe a block but in this case we only have one example so it's only going to run once. So that's going to run and here we have before example. And finally we have the example with the actual basic math being calculated so everything looks proper right now. Well now let's add a little bit more complexity. I'm going to use the context the method which again is just an alias for describe which allows us to describe the condition right a more specific scenario that we're testing. In this case I'm just going to arbitrarily say with condition a we're not actually testing anything real here so this is for the purposes of example we can assume we're testing something in it in a given condition condition. I'm going to provide that context method with a block and I'm actually gonna do is go up here and copy and paste this whole code right here before context and before example but I'm gonna paste it in here and in order to distinguish between them what I'm gonna do is scroll up here and right here before this I'm just gonna write out or before context and outer before example and below I'm just gonna write inner before inner before context and inner before example I'm gonna do some tests in here two tests just to give you a good example here so it lets say it does some more basic math in here I'm going to expect that let's again just do one plus one is going to be equal to two animate a copy that example pasted below and do something like it does subtraction as well. Totally arbitrary here but let's just do five minus three and that should give us two as well. All right. So here's my challenge to you I recommend that you pause the video and before you execute anything in the terminal what I recommend you do is guess or try to figure out logically that's even better of an approach. Try to figure out logically what is going to be output with the put statement and in what order and how is it going to occur. And so take a second to think about it and then I'm going to do is walk you through what's actually gonna happen and then we're going to execute it in the terminal and see what happens. Let's take three seconds to think about it and then I'll rejoin you after you're done. On pausing the video. All right. So we're back. Let's talk about what's going to happen here. The important thing to remember is when we're talking about context we're talking about the current block. So where every you're currently placed where whichever due end construct you're inside that describes your context. So we begin at the most outer level and then we proceed in words in our respect just like we do in most in most programming languages. So in here we start with this higher level block that begins with the described method. And that's this whole thing that captures all of our examples in all of our nested contexts. So that context that whole thing is the beginning of of the top level context. So this line line two to four the outer level context is what's going to run once it's going to turn once a total time because it is the first thing within the current context which is
Рекомендации по теме