Web App Testing & Tools

Unit Testing a Class

Miško Hevery

Miško Hevery

Qwik Creator (Previously Angular)
Web App Testing & Tools

Check out a free preview of the full Web App Testing & Tools course

The "Unit Testing a Class" Lesson is part of the full, Web App Testing & Tools course featured in this preview video. Here's what you'd learn in this lesson:

Miško introduces the concept of a "friendly" while sharing some advice for unit testing a class. A friendly is similar to a mock because it's a set of code that lives inside the test that can be passed objects from the test drive to help control the scope or create edge cases needed to be tested.


Transcript from the "Unit Testing a Class" Lesson

>> I know we talked about it, but I really wanna hammer this home, which is that, let's say you wanna unit test a class, right? So you have your test driver, which is your it method. And you have the class under test or a function under test, it doesn't matter what it is, right?

You have something that you wanna test. And so you say, well, that's pretty simple, I have my test, I will go and poke at the class under test, and things work. Except that there are other classes involved. In our case, the fetch, the delay, right? Those were other externalities, right?

And the thing is, it's not just those externalities, those externalities have a bunch of other externalities. And so very quickly, you get in a situation where you just end up depending on the whole world. And so what you wanna do is you wanna make a seam, right? You wanna have some mechanism by which I can give you the external dependencies so that I can turn them into mocks, right?

And so what you do is you convert this into this, into what we called friendlies. The reason I call them friendlies is because I showed you how to do it with mocks, but oftentimes, people actually create a simplified implementation of the real thing, right? So mocks are one way to do it, and they're pretty straightforward, but you could also create your own implementation for it.

For example, you can create my own fetch that knows how to respond to different things and it's pre-configured with standard responses and response with the headers and error codes and all kinds of other things in there as well, right? You could do that, and then you have a useful thing to just use all over the place.

So these friendlies don't necessarily have to be mocks, they could just be something that you control. And because you control it, the class under test gets sandwiched between both sides of the world that you control. And by sandwiching the unit test in the middle, you then can go and basically control time, control the order in which things resolve.

Create all weird corner cases or race conditions or anything you want can be done in this particular manner. Normally when we write code what we mix is we mix the object graph construction and lookup with business logic. And the thing you need to do is you need to figure out a way to break them apart, right?

So in our case, we did something very, very simple, where we just broke them apart by having default arguments to our class. But you can imagine something more complicated where you have some factory class that actually makes it for you. But the point here is that by breaking these two responsibilities up, you now have the freedom to test things or to move things around or to do things that you didn't have before.

So yeah, in essence, what you want is you wanna have a test driver and you wanna create the friendlies inside of your test and then pass them through somehow to the other side, right? In our case, we had a BeforeEach method that would set up our friendlies, right, our mocks, and pass them to the other side so that we can use it.

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