1 00:00:01,190 --> 00:00:04,660 Time to write out some test to make sure that these events are actually being published. 2 00:00:04,700 --> 00:00:08,180 These tests are going to look very similar to what we had previously written out back inside of our 3 00:00:08,180 --> 00:00:10,360 ticket service just a little bit ago. 4 00:00:10,370 --> 00:00:13,370 Well first start off with our new test test file. 5 00:00:13,400 --> 00:00:17,300 They're going to make sure that whenever a request comes in to create an order we publish any event 6 00:00:17,420 --> 00:00:19,730 saying that an order was created. 7 00:00:19,730 --> 00:00:24,530 I want to give you a very quick reminder you might recall that we have that Nats Rhapsody yes file and 8 00:00:24,530 --> 00:00:28,730 instead of here we have some code that we do not want to be executed inside of our test environment 9 00:00:29,030 --> 00:00:34,250 because it is going to attempt to connect to a real NAT server to get around that problem we had created 10 00:00:34,250 --> 00:00:40,010 that underscore underscore mocks directory inside of our source file and just was going to automatically 11 00:00:40,070 --> 00:00:46,580 redirect any import statements to that Nats wrapper file while we are running our test to this MOX Nats 12 00:00:46,580 --> 00:00:52,980 wrapper file instead so our publishers in the test environment are going to be executing it using this 13 00:00:53,040 --> 00:00:54,410 as the Nats wrapper. 14 00:00:54,480 --> 00:00:58,710 So we're going to have that client object that only implements the publish function and the publish 15 00:00:58,710 --> 00:01:01,140 function is going to be a mock function. 16 00:01:01,180 --> 00:01:05,280 The only goal is publish function right here is to make sure that receives some kind of callback function 17 00:01:05,550 --> 00:01:10,210 invokes it just to trick our code into thinking that the event was published successfully. 18 00:01:10,920 --> 00:01:15,780 So when we go to write out a test around this we're going to make sure that our code after creating 19 00:01:15,780 --> 00:01:21,570 an order eventually invokes this mock function right here and there are some built in matches directly 20 00:01:21,570 --> 00:01:25,410 and just that allow us to make sure that this function actually was invoked. 21 00:01:25,470 --> 00:01:28,220 So let's get to it back inside of our new test. 22 00:01:28,250 --> 00:01:38,920 Yes file we'll go to the very top and we will import the Nats wrapper from up to directories Nats wrapper 23 00:01:40,000 --> 00:01:43,750 so by the time we import this object right here it has already been marked out. 24 00:01:43,780 --> 00:01:49,270 So when we have this import it is actually for this fake Nats wrapper so we can now attempt to make 25 00:01:49,270 --> 00:01:53,560 the request to issue the order recreate the order it is to the event and we should be able to inspect 26 00:01:53,560 --> 00:01:54,580 this object right here. 27 00:01:54,580 --> 00:02:00,180 Make sure that things publish function has been called Let's go down to the bottom where we've got that 28 00:02:00,180 --> 00:02:01,350 little to do is test for it. 29 00:02:01,350 --> 00:02:09,800 Now I'm going to remove the DOT to do on there and then put in an async callback like so so inside of 30 00:02:09,800 --> 00:02:10,020 here. 31 00:02:10,040 --> 00:02:14,060 We definitely want to make sure that we successfully create in order to create an order we can take 32 00:02:14,060 --> 00:02:16,600 the code we have written in the test right before. 33 00:02:16,670 --> 00:02:19,280 So this was some code that would successfully create an order. 34 00:02:19,490 --> 00:02:25,670 I'm going to copy all that and then paste the downside of our test so immediately after we get back 35 00:02:25,670 --> 00:02:31,040 a responsive to a one we should be able to take a look at our Nats wrapper client publish function and 36 00:02:31,040 --> 00:02:38,720 just make sure that it has been invoked so we can write out expect Nats wrapper that client not publish 37 00:02:40,160 --> 00:02:42,860 not to have been called 38 00:02:46,340 --> 00:02:51,710 let's save that go back over to our running tests in our terminal and we're going to expect that the 39 00:02:52,220 --> 00:02:54,390 set of new tests have ran successfully. 40 00:02:54,400 --> 00:02:56,990 Yep looks like everything is passing as usual. 41 00:02:56,990 --> 00:03:03,450 I will change this to fail so I'll do something like not to have been called. 42 00:03:03,530 --> 00:03:08,710 That's how we would say that the Polish function has not been invoked I'll save that put back over 43 00:03:13,130 --> 00:03:17,390 and if we take a look at the air that we just got we said that we expect this thing not to be called 44 00:03:17,720 --> 00:03:21,200 Well it was called the number of times that it was invoked was one. 45 00:03:21,200 --> 00:03:22,340 And here are the arguments to it. 46 00:03:22,950 --> 00:03:26,840 So we got order created as the subject. 47 00:03:27,000 --> 00:03:32,460 We've got the order data inside there and all that kind of good stuff gets us pretty good test right 48 00:03:32,460 --> 00:03:32,740 there. 49 00:03:33,510 --> 00:03:39,150 Let's now implement the same thing for our delete or cancel handler as well inside the delete test. 50 00:03:39,150 --> 00:03:39,660 Yes file 51 00:03:42,580 --> 00:03:44,290 at the very top we'll once again import 52 00:03:47,600 --> 00:03:51,840 Nats rapper from up to directories. 53 00:03:51,840 --> 00:03:56,320 That's rapper well then go down to the very bottom. 54 00:03:56,370 --> 00:04:03,750 I'm going to delete the DOT to do on their would in the async callback and then once again we need to 55 00:04:03,750 --> 00:04:07,980 make sure that we somehow create the order and cancel it and then check to make sure that the published 56 00:04:07,980 --> 00:04:09,430 function has been invoked. 57 00:04:09,510 --> 00:04:15,970 So once again take a look at the test that we wrote right before this one where we had our success case. 58 00:04:16,040 --> 00:04:21,390 So inside of you going to take everything from creating the ticket all the way down to the end of where 59 00:04:21,390 --> 00:04:28,060 we make the request to cancel the order so in total I've got the ticket creation the ticket save reading 60 00:04:28,060 --> 00:04:33,250 the user and then the two different requests I can copy all that and then paste it down at the very 61 00:04:33,250 --> 00:04:34,740 bottom and the tests we just put together 62 00:04:37,780 --> 00:04:47,560 then at the very bottom of our new test we can write out expect Nat's wrapper client publish to have 63 00:04:48,220 --> 00:04:52,670 been built I'll save that. 64 00:04:52,670 --> 00:04:57,350 Take a look at my tests and see how we're doing. 65 00:04:57,400 --> 00:04:59,660 I think I forgot to change back that failing test. 66 00:04:59,680 --> 00:05:01,530 We just wrote inside the new handler. 67 00:05:01,570 --> 00:05:05,590 I'll take care of that in just a moment but right now let's make sure that the cancelled one is working 68 00:05:05,590 --> 00:05:06,520 as expected. 69 00:05:06,520 --> 00:05:11,380 Yep looks like it is going to go back over to a new test. 70 00:05:11,380 --> 00:05:11,800 Yes. 71 00:05:11,810 --> 00:05:14,070 Yeah I still got the knot down there at the very bottom. 72 00:05:14,080 --> 00:05:21,300 I'll just make sure I remove that say this once again and I'll make sure that all my tests are passing. 73 00:05:21,360 --> 00:05:21,630 All right. 74 00:05:21,630 --> 00:05:22,860 Looks good. 75 00:05:22,860 --> 00:05:25,200 So I think we are now all set. 76 00:05:25,200 --> 00:05:28,730 That's pretty much it for publishing some events out of our order service. 77 00:05:28,760 --> 00:05:31,740 So let's take a quick pause right here and decide what we need to work on next. 78 00:05:31,740 --> 00:05:32,460 In just a moment.