1 00:00:00,820 --> 00:00:06,360 We've now got this import redirecting thing or file mocking ads it's referred to with just working. 2 00:00:06,430 --> 00:00:09,400 Now we need to figure out how to write a fake implementation. 3 00:00:09,400 --> 00:00:12,830 Let's take a look at a couple diagrams and help you understand what I really mean by that. 4 00:00:13,250 --> 00:00:19,330 So to write out a fake implementation we really have to take a very deep look at our Nat's wrapper file 5 00:00:19,450 --> 00:00:24,710 the one inside of our SRT directory and we need to understand what this class right here does. 6 00:00:24,760 --> 00:00:28,930 We need to understand what parts of it are used by other files that we are trying to test and then we're 7 00:00:28,930 --> 00:00:34,270 going to provide some fake implementation of just the parts that are expected to be implemented implemented 8 00:00:34,450 --> 00:00:35,720 by those other files. 9 00:00:35,740 --> 00:00:42,000 Again let me just show you a couple of diagrams so we have our new ticket root handler. 10 00:00:42,000 --> 00:00:46,830 That is what we're currently trying to test the new ticket root handler is going to import the Nats 11 00:00:46,890 --> 00:00:48,160 wrapper file. 12 00:00:48,210 --> 00:00:54,090 Now when it imports the real Nats wrapper file it gets an object that looks like this an object that 13 00:00:54,090 --> 00:01:00,300 has an underscore client property that is of type Stan or member Nats client and has a client property 14 00:01:00,570 --> 00:01:05,490 that gives us a Stan or Nats client and a connect function as well. 15 00:01:05,490 --> 00:01:06,870 How do I know that is the object. 16 00:01:06,870 --> 00:01:10,640 Well we can just take a look at the real Nats wrapper file directly. 17 00:01:10,680 --> 00:01:16,800 Here's the real Nats wrapper file inside of here we create a class as you will recall at the very bottom 18 00:01:18,310 --> 00:01:24,530 we create an instance of that class and then we export it as a named export of Nats wrapper. 19 00:01:24,550 --> 00:01:31,240 So what we eventually get inside of our new root handler is an instance of Nats wrapper and an instance 20 00:01:31,240 --> 00:01:38,530 of rats Nats wrapper has underscored clients as the get client and the Connect function doing you now 21 00:01:38,590 --> 00:01:44,260 need to now take a look at how the new ticket root handler actually uses the properties inside of this 22 00:01:44,260 --> 00:01:50,260 object while the new ticket root handler does not care about the underscore client property as a matter 23 00:01:50,260 --> 00:01:52,030 of fact that client property. 24 00:01:52,030 --> 00:01:54,290 The underscore client property is marked as private. 25 00:01:54,460 --> 00:01:59,440 We can not access that thing at all inside of our new ticket root handler but means we really don't 26 00:01:59,440 --> 00:02:00,550 care about it at all. 27 00:02:01,790 --> 00:02:05,420 The new ticket root handler also does not care about the Connect function. 28 00:02:05,420 --> 00:02:10,220 The Connect function only gets in boats from the index not just file when our application is first starting 29 00:02:10,230 --> 00:02:12,410 up when we are trying to actually run it for real. 30 00:02:12,410 --> 00:02:17,090 Not in a test environment and so that leaves just the client property. 31 00:02:17,100 --> 00:02:21,510 This is the only property that our new ticket root handler cares about and that means that this is the 32 00:02:21,540 --> 00:02:27,500 only property that we need to define inside of our little fake implementation let's go into our fake 33 00:02:27,500 --> 00:02:31,460 implementation file right now and we're going to just define this client property. 34 00:02:31,460 --> 00:02:38,590 We won't worry about assigning it a value just yet it's going to find my underscore underscore mocks. 35 00:02:38,760 --> 00:02:45,150 Here's my fake implementation file or the mock file inside this object I'm going to define that client 36 00:02:45,150 --> 00:02:50,020 property so we're gonna come back and provide a value for it in just a moment but before we do we're 37 00:02:50,020 --> 00:02:54,600 going to go through a very similar process where we figure out where this client property is actually 38 00:02:54,600 --> 00:03:01,400 used and we're then going to figure out how to fake that things implementation so we know that this 39 00:03:01,400 --> 00:03:05,420 client property is taken and provided to the ticket created publisher. 40 00:03:05,540 --> 00:03:10,100 So we need to think about how the ticket created publisher actually uses that client. 41 00:03:10,640 --> 00:03:14,940 So let's go and take a look at the ticket created publisher implementation. 42 00:03:15,170 --> 00:03:21,730 We can find that inside of our events publishers ticket created publisher file but here's the publisher. 43 00:03:21,930 --> 00:03:27,750 You'll recall that this thing extends our base publisher class and so the ticket create a publisher 44 00:03:27,840 --> 00:03:30,150 doesn't really do anything with the client directly. 45 00:03:30,450 --> 00:03:35,550 Instead if we want to figure out how that client gets used we need to go and take a little peek at our 46 00:03:35,550 --> 00:03:40,990 base publisher class you'll recall that we implemented that inside of our common module. 47 00:03:41,040 --> 00:03:45,980 Let's go back over to our common module very quickly and take a look at what that publisher really does 48 00:03:45,990 --> 00:03:48,710 with the client. 49 00:03:48,720 --> 00:03:52,030 All right here's my coming module inside there's the SRT directory. 50 00:03:52,050 --> 00:04:00,030 Inside there is events and inside of that is base publisher T.S. but here's the base publisher based 51 00:04:00,030 --> 00:04:07,670 publisher expects to be provided that client property and then internally the only thing it actually 52 00:04:07,670 --> 00:04:13,010 does with it is call the clients publish function when it calls the publish function. 53 00:04:13,040 --> 00:04:14,210 It's going to provide. 54 00:04:14,210 --> 00:04:16,160 We are going to provide three arguments to it. 55 00:04:16,330 --> 00:04:23,740 This subject some string of five data and a callback function then the expectation is that this callback 56 00:04:23,740 --> 00:04:27,470 function is going to eventually be invoked by the client. 57 00:04:27,520 --> 00:04:29,410 That is what the real client does. 58 00:04:29,470 --> 00:04:35,470 The real client is going to eventually call the callback function to indicate that the publish is complete. 59 00:04:35,470 --> 00:04:39,890 So with that in mind a couple more diagrams. 60 00:04:40,230 --> 00:04:42,170 Sorry I should've showed you this one just a moment ago. 61 00:04:42,270 --> 00:04:46,920 Purpose of this one was to just say we have our new ticket root handler it imports the Nats wrapper 62 00:04:47,190 --> 00:04:50,220 and the only thing that it cares about inside there is the client and the client gets passed off to 63 00:04:50,220 --> 00:04:50,910 the publisher. 64 00:04:50,910 --> 00:04:54,370 We just discussed that it's now inside of our base publisher. 65 00:04:54,420 --> 00:04:59,040 This thing takes the client it has a published function the base publisher has a published function 66 00:05:00,150 --> 00:05:05,220 and the base publisher is going to try to invoke this client publish and it's going to call it with 67 00:05:05,220 --> 00:05:07,900 some subject data and a callback. 68 00:05:08,040 --> 00:05:13,910 And the expectation is that that callback is going to be invoked after the event is successfully published. 69 00:05:13,920 --> 00:05:22,630 So for us to continue to implement this kind of fake or mock class we need to make sure that let's go 70 00:05:22,630 --> 00:05:24,940 back to our mock file really quickly. 71 00:05:24,940 --> 00:05:31,910 We may need to make sure that this client thing right here is an object that has a Polish function the 72 00:05:31,910 --> 00:05:37,970 published function that needs to take in a subject that is a string data that is a string and a callback 73 00:05:37,970 --> 00:05:38,900 function. 74 00:05:39,020 --> 00:05:43,170 And as soon as we receive that callback function we should invoke it right away. 75 00:05:43,280 --> 00:05:44,570 So we're gonna do that right now. 76 00:05:45,850 --> 00:05:47,870 We're gonna find client right here. 77 00:05:47,910 --> 00:05:49,750 This needs to be an object. 78 00:05:49,920 --> 00:05:57,200 It needs to have a public function to define this as a ERO function like so and this public function 79 00:05:57,290 --> 00:06:03,710 needs to take in a subject that is a string data that is a string and a callback function that is a 80 00:06:03,710 --> 00:06:04,130 function 81 00:06:07,250 --> 00:06:15,550 and then the expectation is that inside this Polish function we are going to call callback right away. 82 00:06:15,650 --> 00:06:20,780 And that is it if we run this code right now our test for the new root handler should pass. 83 00:06:20,780 --> 00:06:24,800 But before we do just want to make sure it's super crystal clear what is going on here because this 84 00:06:24,800 --> 00:06:26,540 is some confusing stuff. 85 00:06:26,540 --> 00:06:29,100 They're just one more time to run through the entire setup. 86 00:06:29,360 --> 00:06:35,890 We have built out this mock file when our tests are executed in the test environment our new root handler. 87 00:06:35,900 --> 00:06:42,160 Here's the new handler is going to import that fake or the mock Nats wrapper file out of that file. 88 00:06:42,170 --> 00:06:45,530 It's going to look for a named export of Nat's wrapper. 89 00:06:45,530 --> 00:06:53,890 So the new root handler is going to get this whole object right here then inside the new root handler. 90 00:06:54,010 --> 00:06:56,680 We're going to take that Nats wrapper object. 91 00:06:56,860 --> 00:07:02,290 We're gonna pull off the client property and we're going to provide it to the get created publisher. 92 00:07:02,350 --> 00:07:09,310 So in other words the ticket created publisher is going to receive just this object right here. 93 00:07:09,320 --> 00:07:10,360 That is the fake client. 94 00:07:10,370 --> 00:07:16,380 We just put together so that little object just this object with the publish function is going to show 95 00:07:16,380 --> 00:07:23,240 up inside of our base publisher as client the base publisher is gonna take that client assign it to 96 00:07:23,240 --> 00:07:29,600 the best client property and then at some point time our handler is going to call this publish function 97 00:07:29,600 --> 00:07:34,250 right here to try to publish some event internally. 98 00:07:34,270 --> 00:07:36,890 This thing is going to create a new promise and return it. 99 00:07:36,890 --> 00:07:42,700 And inside that promise it's going to access this client not publish this client not publish. 100 00:07:42,700 --> 00:07:46,550 That is the basic function we just put together right here. 101 00:07:46,570 --> 00:07:48,840 That is what is actually going to get executed 102 00:07:52,050 --> 00:07:56,520 so it's gonna call this duck client publish provide a subject provide the data and this callback function 103 00:07:57,240 --> 00:08:00,380 and then we want to make sure that the callback function gets executed right away. 104 00:08:00,390 --> 00:08:06,720 So the overall promise gets result and that's why inside of our fake implementation right here we receive 105 00:08:06,750 --> 00:08:12,300 the subject the data and the callback function and then inside all we did was all the callback. 106 00:08:12,300 --> 00:08:15,240 As soon as we possibly could. 107 00:08:15,430 --> 00:08:16,300 So let's save all this. 108 00:08:16,300 --> 00:08:22,540 We're gonna go back over to our terminal an LLC that our new test out handler as all of its test passing 109 00:08:23,740 --> 00:08:26,880 looks pretty great if we try to run all of our tests now. 110 00:08:26,890 --> 00:08:33,140 So if I do a W to see the watch menu I can then press a on my keyboard to run all tests. 111 00:08:33,290 --> 00:08:38,310 They'll do that and we are going to see still a lot of tests failing. 112 00:08:38,310 --> 00:08:38,880 There we go. 113 00:08:38,880 --> 00:08:40,410 So still a lot of tests failing. 114 00:08:40,410 --> 00:08:47,960 The reason for that is that we now have you might recall inside of our new test file we have told the 115 00:08:48,050 --> 00:08:51,250 new test file or just to try to block that file. 116 00:08:51,290 --> 00:08:54,720 But we have other test files that do not have the same mock statement. 117 00:08:54,800 --> 00:08:58,740 So we have to go round to our other test files and tell all of our other tests. 118 00:08:58,780 --> 00:09:03,060 Hey if you ever tried to import the Nats wrapper file instead do a redirect. 119 00:09:03,080 --> 00:09:05,300 Get this other fake file. 120 00:09:05,360 --> 00:09:08,510 Let's take a pause right here and copy paste thing around in just a moment.