Time to write out some test to make sure that these events are actually being published.
These tests are going to look very similar to what we had previously written out back inside of our
ticket service just a little bit ago.
Well first start off with our new test test file.
They're going to make sure that whenever a request comes in to create an order we publish any event
saying that an order was created.
I want to give you a very quick reminder you might recall that we have that Nats Rhapsody yes file and
instead of here we have some code that we do not want to be executed inside of our test environment
because it is going to attempt to connect to a real NAT server to get around that problem we had created
that underscore underscore mocks directory inside of our source file and just was going to automatically
redirect any import statements to that Nats wrapper file while we are running our test to this MOX Nats
wrapper file instead so our publishers in the test environment are going to be executing it using this
as the Nats wrapper.
So we're going to have that client object that only implements the publish function and the publish
function is going to be a mock function.
The only goal is publish function right here is to make sure that receives some kind of callback function
invokes it just to trick our code into thinking that the event was published successfully.
So when we go to write out a test around this we're going to make sure that our code after creating
an order eventually invokes this mock function right here and there are some built in matches directly
and just that allow us to make sure that this function actually was invoked.
So let's get to it back inside of our new test.
Yes file we'll go to the very top and we will import the Nats wrapper from up to directories Nats wrapper
so by the time we import this object right here it has already been marked out.
So when we have this import it is actually for this fake Nats wrapper so we can now attempt to make
the request to issue the order recreate the order it is to the event and we should be able to inspect
this object right here.
Make sure that things publish function has been called Let's go down to the bottom where we've got that
little to do is test for it.
Now I'm going to remove the DOT to do on there and then put in an async callback like so so inside of
here.
We definitely want to make sure that we successfully create in order to create an order we can take
the code we have written in the test right before.
So this was some code that would successfully create an order.
I'm going to copy all that and then paste the downside of our test so immediately after we get back
a responsive to a one we should be able to take a look at our Nats wrapper client publish function and
just make sure that it has been invoked so we can write out expect Nats wrapper that client not publish
not to have been called
let's save that go back over to our running tests in our terminal and we're going to expect that the
set of new tests have ran successfully.
Yep looks like everything is passing as usual.
I will change this to fail so I'll do something like not to have been called.
That's how we would say that the Polish function has not been invoked I'll save that put back over
and if we take a look at the air that we just got we said that we expect this thing not to be called
Well it was called the number of times that it was invoked was one.
And here are the arguments to it.
So we got order created as the subject.
We've got the order data inside there and all that kind of good stuff gets us pretty good test right
there.
Let's now implement the same thing for our delete or cancel handler as well inside the delete test.
Yes file
at the very top we'll once again import
Nats rapper from up to directories.
That's rapper well then go down to the very bottom.
I'm going to delete the DOT to do on their would in the async callback and then once again we need to
make sure that we somehow create the order and cancel it and then check to make sure that the published
function has been invoked.
So once again take a look at the test that we wrote right before this one where we had our success case.
So inside of you going to take everything from creating the ticket all the way down to the end of where
we make the request to cancel the order so in total I've got the ticket creation the ticket save reading
the user and then the two different requests I can copy all that and then paste it down at the very
bottom and the tests we just put together
then at the very bottom of our new test we can write out expect Nat's wrapper client publish to have
been built I'll save that.
Take a look at my tests and see how we're doing.
I think I forgot to change back that failing test.
We just wrote inside the new handler.
I'll take care of that in just a moment but right now let's make sure that the cancelled one is working
as expected.
Yep looks like it is going to go back over to a new test.
Yes.
Yeah I still got the knot down there at the very bottom.
I'll just make sure I remove that say this once again and I'll make sure that all my tests are passing.
All right.
Looks good.
So I think we are now all set.
That's pretty much it for publishing some events out of our order service.
So let's take a quick pause right here and decide what we need to work on next.
In just a moment.