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

The "Writing 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 introduces the exercise and sets up the local environment for coding. She writes unit tests for the shop's default items, such as checking the name, sell-in value, and quality value of items in a shop. Francesca also demonstrates how to group tests using describe blocks.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Writing Tests" Lesson

[00:00:00]
>> Francesca Sadikin: So now let's get started on our exercise. So as a recap, what we're going to be doing in this exercise is to work an existing code. So this is going to be very similar to what you might be doing in your companies. So just to introduce you to what I have set up in my local environment, I have all of the shop requirements here on the left, including the new one that we need to implement about conjured items.

[00:00:29]
In the test files, I have these little buttons called run and debug. This is coming from just runner vs code extension. Essentially, it's just Is an easy way for me to enter a debug mode in these tests. You don't have to have this set up for yourself. I will show you the debugger as part of this exercise later on.

[00:00:53]
And I also have my terminal down here. I am already in the js-jest directory so, that means that we're working in JavaScript with the jest testing framework, and I'm gonna run the test suite by running NPM run test, colon: watch. This runs it in test watch mode, so you can see it run here.

[00:01:18]
And every time I make a save in any of my files, you can see that it just reruns for me.
>> Francesca Sadikin: Okay, so let us start. If you guys are not familiar with just to give you also another little overview of what we're looking at here. This is the test block itself, right?

[00:01:42]
So it says, it starts with an it, the first parameter is the name of the test, and then we have a callback in which we set up our test and in this case, we are creating our new gildedrose shop with a single item of foo and also if I hover over item, you can see that it's providing me the names of the different arguments.

[00:02:07]
So the second argument is selling and the third is quality. I am calling update quality of the shop, which returns the set of items. And then here is the actual test assertion, where in this case I'm just grabbing the first item in this items collection, checking that the name is currently fixme.

[00:02:31]
So this first test is failing. And so let's actually make this pass, right? So it's telling us that it expected fixme, so that's what we wrote here, but it received foo. So to make this pass, I'm just going to change this back to foo. And so this one now passes.

[00:02:52]
So I know this is a test that they just provided to help us get started. But I actually think this is a useful test we can keep. We are just going to rename it, because really, it's just checking that there is a name property on the item. So I'm going to rename this as should have a name value, all right.

[00:03:15]
So I'm going to now go down this list on the left and right new unit test. The next one is that all items have a cell and value, which denotes the number of days we have to sell the item. I'm going to combine that with this requirement right here that says that at the end of the day, the system lowers both the sell and end quality values for every item.

[00:03:42]
So I'm going to write a new test block and I'm going to call it, it should have a cell in value that decreases at the end of the day. So it should have a cell in value that decreases at the end of the day.
>> Francesca Sadikin: Just something to note here, you can see that I am starting all of my names with it should.

[00:04:09]
This is a little just naming convention where we start with should and then the grammatically correct sentence of what this test does. So Inside, I'm just going to create my new gildedrose shop, which has a single item. It doesn't quite matter what the name of this item is, so I'm just going to use foo, and I'll use a selling of 10 and a quality of 20.

[00:04:35]
I'm going to call gildedrose update quality which returns the items, and now I expect that the first item's selling is going to be 9, cuz it's going to decrease by 1. Okay, so we have that test, and then so we're gonna write our next one, very similar, except it's going to be about the quality value instead.

[00:05:03]
So it should have a quality value that decreases at the end of the day.
>> Francesca Sadikin: Going to create my new gildedrose shop. Single item of food use the same selling, same quality value. I'm gonna create my items from gildedrose, update quality, and this time I am asserting that the first items quality is going to be 19.

[00:05:40]
Okay, so just a little forewarning, it can get a little tedious to write all of these tests, but stay with me, because I'm gonna show you how powerful this test suite is going to be once we enter our refactoring phases. All right, we have two more for default items.

[00:05:58]
Once the sell by date has passed quality degrades twice as fast. So I'm gonna convert that into the name. It should degrade quality twice as fast when selling has passed. So it should degrade quality twice as fast once selling has passed.
>> Francesca Sadikin: So inside, I'm creating my new gildedrose shop with the single item of foo, except this time, I'm going to use a sell in of 0 to mark it as a sell by date has that has passed, and then I'm gonna use quality of 20 going to check the items from update quality.

[00:06:46]
And this time, I'm going to expect that the items, well let me check that the sell in is continuing to decrement. So in this case, I expect -1. Yep, that's right. And then now I check that item's quality. So twice as fast means that it's going to be 18, okay.

[00:07:11]
The last one here for regular items is that the quality of an item is never negative, so it should never have a negative value. That's what I'll call it should never have a negative value, quality value.
>> Francesca Sadikin: Here's my little setup of the gildedrose shop.
>> Francesca Sadikin: And this time I will still use foo as the name, selling is 10, accept, and then the quality is now 0, so that's what I'm checking.

[00:07:56]
So const items = gildedrose update quality.
>> Francesca Sadikin: And so I expect that the items quality is going to be 0, it's not going to continue decrementing, okay. So I have the first set of tests and I can see that we have quite a few specialty items. And what I want to do is start grouping these tests together.

[00:08:28]
So you notice these describe blocks, all it's doing is just grouping the tests. That's all it is. And so I'm going to say that this is the default item behavior suite, I'm going to move all of my tests so far.
>> Francesca Sadikin: I clicked Option up on a Mac, and that just moved all the lines up for me.

[00:08:52]
And so now you can see that new grouping, all of these tests are under this new title here. So that's all it's doing.

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