Check out a free preview of the full Testing Fundamentals course

The "Spies" Lesson is part of the full, Testing Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Steve demonstrates how spies track whether a function was called, how many times it was called, and with what arguments. They can also verify that a function was triggered under the right conditions. Spies do not modify the function’s original behavior unless explicitly configured.

Preview
Close

Transcript from the "Spies" Lesson

[00:00:00]
>> Steve Kinney: So let's just look at some of these in practice. So here I've got that little scratchpad. So we can do something really simple to demonstrate the point here, which is there are really two things, you could break into stubs also. I'm gonna break this into two things, mocks and spies.

[00:00:20]
A mock is a function that you plan on just being a placeholder for something else, right? It replaces whatever you were gonna call. A spy keeps what you had before, but then wraps it in some abilities to have some introspection on that. Those are words. Let's actually see it in practice because otherwise, I could talk about this for another hour, you just stare at me blankly.

[00:00:49]
So let's look at some code. Cool, so let's start with, if you don't have the globals, you can pull in this vi. A lot of times, so I put the globals on cuz, one, it was just and to because that way you didn't have to worry about it.

[00:01:06]
I tend to import them in every test in my code base because then the IntelliSense is better, that's all. You can do it either way. So here we can do vi.spyOn. So this is gonna say, hey, here's an existing function that exists that don't change it, don't do anything different.

[00:01:31]
But I wanna look at it later, right? I wanna keep an eye on this one. So we're gonna take the console object and we're gonna take this log method. Are you familiar with console log? It's the greatest debugging tool ever invented. So we'll spy on it, and we'll call this our log spy, right?

[00:01:59]
And then, again, this is just a little playground that we can play in. So what we'll say is,
>> Steve Kinney: And that's the same console log that we know and love. Nothing has changed. But now we can do stuff like,
>> Steve Kinney: Let's open it in here.
>> Steve Kinney: Right, and so this test passes.

[00:02:44]
So our console log, let's see, we still logged, it still did the thing, right? But before it hit the actual console log, we hugged it with our own little special function, right, in logsSpy which is like, go look at every call to console log and just keep track of it, right?

[00:03:03]
And so console log2 have been called. And so if we didn't call it, it fails, right? So if you had something that was maybe writing to a canvas, right? Or something along those lines or maybe it is supposed to console log or maybe it's supposed to trigger an alert or something along those lines, right?

[00:03:26]
And you just wanna make sure that that side effect happened, you can do that, right? So it should have called console log. Okay, that one's good, what about,
>> Steve Kinney: So then you can also say, okay, but did it get called with the thing I think it was supposed to get called with, right?

[00:03:57]
In this case, obviously, in this dumb example, that will pass. And so all that spy does is it doesn't replace anything. It doesn't change anything. It just goes ahead and makes sure that if your function had some kind of side effect, the call to the outside world that wasn't in the return value and you need to make sure that it happened, guess what a spy is good for.

[00:04:20]
It's good for making sure that that happened. And you can even do stuff like,
>> Steve Kinney: Let's say you're worried about calling the API too many times, right?
>> Steve Kinney: Right, these are all effectively ways to make sure that these side effects that you can't get your hands on otherwise were called the way that you think that they should have been called.

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