Tip:
Highlight text to annotate it
X
JOHN LINDQUIST: Before I go any further, I really need to
introduce the concept of testing.
So we're going to test our filter to make
sure that it works.
Now, this is going to be the only video on testing.
It's just going to be kind of a broad overview of what you
should generally do.
But it's necessary because when you start writing things
like filters, you need to have absolute confidence that
they're doing what you expect them to do, not just that
they're magically working.
So let's delete our filter.
And we're going to use the Testacular test runner with
the Jasmine testing framework.
And again, I'm not going to show you how to set this up.
There's other videos on that, and I'm planning on doing a
separate video course on it.
But for right now, I just want to again just do an overview
of what it looks like.
So we're just going to describe a test suite of what
our filter should do.
And basically, we're going to need our module of my app to
access any of those filters.
And so the specific filter we want to talk about is the
reverse filter.
And then, let's talk about our expectations
here and our specs.
So our reverse filter should reverse a string.
And we're going to inject our reverse filter into this, so
it will inject, and then it requires a function of our
reverse filter.
Now basically, at this point, we write our expectations.
And we say things like expect our reverse filter to take the
string of ABCD, and that will equal DCBA.
And so, that's kind of a full test there saying that we
should have a reverse filter, and it should
take a string of ABCD.
And then it should reverse it to DCBA so that if we run
this, you'll see that quite a few things have failed.
So failing in unit tests and testing is actually a good
thing because it gives you the next step of
what you need to do.
So we don't have a reverse filter, so we
need to set one up.
So that's basically what that test says, or what that
failure was telling us to do, is just go up and set up a
reverse filter.
So we'll create our reverse filter here.
And if you remember the syntax--
all right.
So now, let's clear that out and run it again.
So our error of reverse filter doesn't exist as gone.
And now it says filter reverse should reverse a string.
So that's saying filter reverse
should reverse a string.
And it got back nothing and expected DCBA.
So we handed back nothing, and it wanted DCBA.
So let's make that pass DCBA.
And this is kind of cheating, but it's actually a good thing
because you want to do the smallest step to get
your test to pass.
So now we're back to green.
We've gone from the red cycle to the green cycle.
But we know that we're really cheating here and that this
isn't enough test coverage to really guarantee that that's
reversing anything.
So we also want to expect that JOHN will reverse to NHOJ.
So JOHN to NHOJ, or whatever.
And then, if I run the tests again, it's
going to fail again.
So you should reverse a string.
We expected NHOJ.
We got back DCBA.
That's not correct.
So now let's actually fulfill the requirements here, and
we'll return the input split and reverse and
join them back together.
Now, if we run the test again, you'll see
that the test passed.
So now we're confident that based on our tests here, our
expectations of ABCD and JOHN, that those will be reversed.
So we're pretty darn confident that any string that we're
going to pass in is going to be reversed.
You could write some more expectations around special
characters and those sorts of things to make sure you have
full coverage if you start seeing other errors pop up.
But basically, that's what unit testing is.
It's just taking the smallest things you can find in your
code base, writing some very simple code around them to
say, is this working as expected?
And if it is, then you have automatic code you can run to
guarantee that your code is working as expected.
Hopefully, that's helped you understand unit
testing a bit more.
I'm not going to go much more into it as
I go through AngularJS.
But I will cover it in the future, so
don't worry about that.