Check out a free preview of the full Software Developer Success: Soft Skills & Testing course

The "Conjured Items Tests" Lesson is part of the full, Software Developer Success: Soft Skills & Testing course featured in this preview video. Here's what you'd learn in this lesson:

Francesca continues refactoring the code by extracting the handlers for different items into separate functions. She also introduces a new item called "conjured" and writes tests and code to handle its specific behavior.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Conjured Items Tests" Lesson

[00:00:00]
>> Francesca Sadikin: So conjured items, the thing we were supposed to do. Here we go, we're going to start with the test.
>> Francesca Sadikin: Let's see, too many described blocks, right. There we go, so I'm going to create a new describe block. This is called conjured items.
>> Francesca Sadikin: And let's read this again.

[00:00:26]
So, conjured items degrade in quality twice as fast as normal items. So, based off of this wording, my interpretation is that it should behave like a regular regular item, the only difference is that it degrades the quality twice as fast versus by one, right, in this case. I'm going to say that it should degrade quality twice as fast as normal items.

[00:01:01]
>> Francesca Sadikin: So I'm gonna create my new gildedRose shop, and inside of it I'm gonna create multiple items this time. Because the immediate thing that I'm thinking about in terms of edge conditions is, I need to handle casing, like I wanna make sure this works, whether it's. Lowercase, uppercase, capitalized.

[00:01:23]
So I'm gonna start off with conjured cat with the selling of five and a quality of 10. Then add other items here, I'm gonna do capitalized conjured cup selling 10. Quality 20, and then a third one doing uppercase CONJURED hat with selling of 15, quality of 30. So now I'm going to grab items off of gildedRose.updateQuality.

[00:01:57]
And now I can, let's check the sellIn, just making sure that it really, we're going to be decrementing it as expected. So the first item's sellIn is going to be 4. We passed, yeah, because it's just going through the default handler. So item (0).quality is now going to be 8 and now it's failing, okay.

[00:02:27]
So now this is where we should pause and just make this pass first. So I'm gonna close this side window, open this up back to the right. Take a look here, to make conjured items past, I'm going to create a new handler. Right, we're gonna continue this pattern here.

[00:02:52]
Sorry, not handler, I'm gonna call it conjured. It takes an item in the function and so far, we've determined that the items sellIn continues to decrement by 1 and that the quality now decrements by 2. I click save, nothing happens because we have to actually use this handler.

[00:03:17]
So I can see that, the switch between the handlers happen at this line. And I could keep adding to this conditional right here, but it's starting to get a little overloaded. And so, I'm actually going to extract this out into a new function. I'm gonna call this one, getHandlerName, which takes an item.

[00:03:43]
And so I'm gonna transfer this over, so the default case is that it always returns default, right? Then here's the next one. We add if object has own item, handler, item name, we return item.name. And then just to make sure that everything's working, I'm just going to pass this in, call this right here.

[00:04:09]
Yeap, everything's still passing except for our new one. So now let's actually write the new logic to make it switch to conjured. So simple enough, we just check that the name includes conjured, okay? And then we return conjured. Okay, so that test is passing, but we have a few more to take care of, let's check the next items, quality, so to be 18, okay.

[00:04:47]
Now this is failing again, and that is because, as we thought, we needed to handle the casing. So that's simple enough, the plan is to convert this all to a lower case. Before we check that conjured is inside. So now that passes, and I believe that that handles the next case as well.

[00:05:09]
Quality to be 28, there we go. Okay, so we have a few more, though. The other behavior of a default item is that it should degrade quality twice as fast. So once sellIn has passed. I'm going to create a new one that says, it should degrade quality twice as fast when sellIn has passed.

[00:05:37]
That's fine, I will keep that name. Should degrade quality twice as fast once sellIn passed.
>> Francesca Sadikin: So gonna create my new gildedRose shop. Going to check that quality once that's passed. Okay, so gonna give it conjured cat again. SellIn is 0, and now quality is 10. So if I grab items off of gildedRose.updateQuality, I now expect that that first item's quality is going to be, so doubling of what.

[00:06:20]
The normal one was is like decreasing it by 4, so I expect it to be 6. So this is failing because we actually need to write the code to make this pass. So we come back up here to the handler. What I'm gonna do, just wrap this another logic here.

[00:06:42]
If item, sellIn is greater than 0, we decrement by 2, otherwise we decrement by 4. Okay, so this is passing now we have yet another one. In that we should never have a negative quality value. So, should never have a negative quality value.
>> Francesca Sadikin: Well, syntax, here we go.

[00:07:18]
Here's my new gildedRose Shop. Single, let's put an item here called conjure cat. We have a sellInof, we can have any positive 1, so like a 2. And this time I wanna check that like even if we subtract by 2 or 4 in these cases It will never go past 0.

[00:07:43]
So, I'm gonna give it 1 instead and I'm now going to check that, if we call updateQuality we spec that first items quality to be 0, right? Cuz this was going to just subtract by two, but we wanted to make sure that it's always gonna be zero. I think I have a syntax issue, dildedRose yeah, a bunch of stuff outside of the test block.

[00:08:17]
There we go, here we go. Okay, yes. So, now we are receiving -1, but we expected 0. So we can make that code work again. We can just say if item.quality is less than 0, we set it back to 0, okay? So, we have our test passing and before we wrap this up.

[00:08:41]
I'm actually noticing one more round of refactoring. I'm noticing that this and this is very similar, right? It looks almost exactly the same. The only difference is that it decrements by 1 and 2 versus 2 and 4. So I am actually going to do another refactor to try and combine these two functions together.

[00:09:10]
So what I would ideally like to do to call here is, I wanna call ItemHandler. I want to call the default one and give it the Value, right? So, in this case I'm like I want it to start by 2, I'm going to click Save this is gonna break a bunch of stuff because I didn't actually make this work yet, but yeah you can see the conjured items it's not being handled.

[00:09:38]
So, we need to adjust the default function now. So I said I wanted to give it a value, so I'm going to provide another argument called decrementValue which I will default to 1. So that in all the other cases if no one passes is anything it's going to work as normal when I use that here, and then multiply it by 2 here.

[00:10:04]
And so now we have a cleaned up conjured handler and we're just going to reuse the default handler's logic, okay, that is it. That is the exercise, [LAUGH] we're done, we're finally done.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now