1 00:00:06,570 --> 00:00:06,890 Hello. 2 00:00:08,220 --> 00:00:13,140 In this video, we're going to talk about a very common question from interviews. 3 00:00:13,470 --> 00:00:14,820 Question 46. 4 00:00:15,030 --> 00:00:23,580 What are mooks mocks are objects that pretend to be other objects, and they're used mostly for testing 5 00:00:23,580 --> 00:00:24,390 purposes. 6 00:00:24,780 --> 00:00:31,890 For example, we don't want to use a real database connection in unit tests, and we'll explain why 7 00:00:31,890 --> 00:00:32,760 in a minute. 8 00:00:33,180 --> 00:00:40,830 Instead, we will replace the object connecting to a database with a mock that provides the same interface, 9 00:00:40,980 --> 00:00:42,720 but returns this data. 10 00:00:43,290 --> 00:00:48,000 Let's say we wanted to test this glass emerging. 11 00:00:48,000 --> 00:00:55,170 This method connects to our audio database, performing all necessary steps like opening the database 12 00:00:55,170 --> 00:01:03,270 connection, executing some obscure queries, etc. The unit tests for this class could look like this. 13 00:01:04,550 --> 00:01:09,770 This test simply executes the format method when this method will run. 14 00:01:09,980 --> 00:01:15,380 It will try to connect to other database searching some people object and formatting them. 15 00:01:16,010 --> 00:01:22,670 This test could even work under some circumstances, but there are numerous problems with this approach. 16 00:01:23,030 --> 00:01:28,280 First of all, a test that connects to a database is not a you need to test. 17 00:01:28,670 --> 00:01:32,300 A unique test should test only one piece of functionality. 18 00:01:32,570 --> 00:01:37,730 Here we test the class, the database connection and the database itself. 19 00:01:38,090 --> 00:01:43,520 Also, unique tests should be fast, and connecting to a database takes time. 20 00:01:44,210 --> 00:01:46,970 This test only reads from the database. 21 00:01:47,090 --> 00:01:54,380 But what if other tests would also write do it if some of test would add a new person to their database? 22 00:01:54,500 --> 00:02:02,090 This test would start to fail as the result would contain one more line as this would run the database 23 00:02:02,090 --> 00:02:05,480 state, with giant constantly affecting the results. 24 00:02:05,840 --> 00:02:12,470 Because of that, we would be forced to reset the database to some desired state before each test, 25 00:02:12,680 --> 00:02:15,530 which would again take significant time. 26 00:02:15,890 --> 00:02:21,530 Also, what if the database is not set up on the computer of another developer? 27 00:02:21,980 --> 00:02:26,600 This test may work for us, but it may not work for others. 28 00:02:27,050 --> 00:02:31,280 Finally, what if the database contains millions of entries? 29 00:02:31,580 --> 00:02:37,790 Then the result of the format method would be a normal string, which would obviously be problematic, 30 00:02:37,940 --> 00:02:39,700 especially if the test failed. 31 00:02:39,710 --> 00:02:46,160 And in this huge string, we would try to find the exact part that doesn't match the expected result. 32 00:02:46,640 --> 00:02:53,690 To solve all those issues, we need a mechanism that will allow us to mock the database connection instead 33 00:02:53,690 --> 00:02:57,060 of using an object connecting to our database. 34 00:02:57,110 --> 00:03:03,830 We'll use a fake one that will return a predefined set of data used for testing purposes only. 35 00:03:05,140 --> 00:03:12,070 But first, we must refactor this code to use dependency injection, so we are not tightly coupled with 36 00:03:12,070 --> 00:03:15,460 the implementation that connects to a real database. 37 00:03:30,140 --> 00:03:36,890 Here, I injected an object implementing open data reader interface to the production code. 38 00:03:37,970 --> 00:03:44,840 We will use this implementation would actually connect to a database, but for unique tests, we'll 39 00:03:44,840 --> 00:03:45,980 use AMOC. 40 00:03:47,690 --> 00:03:54,500 I will be using the mock library for that, which is one of the most popular mocking libraries for C-sharp. 41 00:03:55,070 --> 00:03:58,280 This is how we can create a mock up of some interface. 42 00:04:21,060 --> 00:04:26,670 As you can see, I moved the creation of the cross under test object to the suck up method. 43 00:04:27,210 --> 00:04:33,480 This is because I want a brand new mark for each test, which is a good practice since the mocks have 44 00:04:33,480 --> 00:04:34,440 their own state. 45 00:04:34,650 --> 00:04:41,640 They use it mostly to track what methods have been called upon them, which is used for validating behavior. 46 00:04:42,120 --> 00:04:45,180 We'll talk more about it later in this sector. 47 00:04:45,720 --> 00:04:48,210 Let's not use this mock in the test. 48 00:04:48,750 --> 00:04:53,340 I will set it up to return some predefined people objects when they reach people. 49 00:04:53,340 --> 00:04:54,780 Method it is gold. 50 00:05:22,500 --> 00:05:25,380 First of all, let's make sure this test buses. 51 00:05:28,370 --> 00:05:28,930 Great. 52 00:05:29,300 --> 00:05:30,620 The dust has passed. 53 00:05:31,520 --> 00:05:35,010 Let's make sure we understand what happened here. 54 00:05:35,030 --> 00:05:41,120 I set up the mosque, so it returns this collection of people when the rich people method is caught 55 00:05:41,120 --> 00:05:42,200 on this object. 56 00:05:42,590 --> 00:05:46,130 This mosque was injected into grass under test object. 57 00:05:46,310 --> 00:05:50,780 So when we called the format method on this object, this is what happens. 58 00:05:52,280 --> 00:05:57,080 This object is actually a mock, and it doesn't connect to our database. 59 00:05:57,560 --> 00:06:04,490 This method will return the collection I defined and it will be used when the people's data is formatted. 60 00:06:05,690 --> 00:06:14,030 Finally, I simply compare the result with the expected result using a mock solved all problems we mentioned 61 00:06:14,030 --> 00:06:14,660 before. 62 00:06:15,110 --> 00:06:16,880 Now this test is a real. 63 00:06:16,910 --> 00:06:22,160 You need to test it, test the personal data for model class in isolation. 64 00:06:22,730 --> 00:06:26,210 It is fast because it doesn't connect to a database. 65 00:06:27,050 --> 00:06:32,510 It has no way of affecting other tests, as it doesn't modified and the charts state. 66 00:06:32,870 --> 00:06:38,900 If we had a test that doesn't use marks and it would write to a database, it would modify the database 67 00:06:38,900 --> 00:06:41,240 content for all of our tests. 68 00:06:41,990 --> 00:06:44,780 Also, this test will work on any machine. 69 00:06:44,960 --> 00:06:48,500 No matter if some database is present on it or not. 70 00:06:49,040 --> 00:06:52,010 Finally, we have full control over the data. 71 00:06:52,370 --> 00:06:57,800 We defined a small set of people that is enough for testing the personal data, for matter. 72 00:06:58,220 --> 00:07:03,680 We won't be affected by the fact that there can be millions of people in the database. 73 00:07:04,400 --> 00:07:05,150 All right. 74 00:07:05,390 --> 00:07:09,140 Please notice that mosques have one more powerful ability. 75 00:07:09,530 --> 00:07:15,410 We can verify if some methods have been called upon them as part of the test verification. 76 00:07:15,920 --> 00:07:17,720 Let's consider this class. 77 00:07:19,360 --> 00:07:27,010 This class is quite simple, but unfortunately it is not easy to test the print hello and transmitted 78 00:07:27,010 --> 00:07:27,820 is void. 79 00:07:27,970 --> 00:07:31,720 So there is no result to be compared with the expected result. 80 00:07:32,290 --> 00:07:38,440 The test of this method should basically have a way of checking if the whole thing was printed to the 81 00:07:38,440 --> 00:07:45,880 console, given the amount of times it could possibly be done by actually running the program, which 82 00:07:45,880 --> 00:07:52,510 of course, would make this test known unit and somehow intercepting the output printout to the console. 83 00:07:52,840 --> 00:07:56,350 But these would be complex, tricky and non unitary. 84 00:07:56,860 --> 00:08:02,650 After all, we would be testing the console class as much as the enthusiast in a greater class. 85 00:08:03,250 --> 00:08:05,680 The solution is again to use mock. 86 00:08:06,130 --> 00:08:08,500 But what do mock fear exactly? 87 00:08:09,070 --> 00:08:16,260 Well, ideally it would be to mock the console class, but it is impossible since this class is static 88 00:08:16,570 --> 00:08:18,850 in most frameworks, including mock. 89 00:08:19,090 --> 00:08:24,880 The mocking mechanism is based on inheritance or on implementing some interface. 90 00:08:25,150 --> 00:08:32,140 So a mock object is basically a derived type from the type we want to mock, or it implements the mocked 91 00:08:32,140 --> 00:08:33,040 interface. 92 00:08:33,610 --> 00:08:41,560 We can't have classes derived from static classes, so it's not possible to mock the console class again 93 00:08:41,560 --> 00:08:44,290 willing to use the dependency injection. 94 00:08:44,620 --> 00:08:48,430 I wanted to inject the logic of printing to the console. 95 00:08:48,880 --> 00:08:55,120 Printing to the console is simply a method that takes some string so I can do it by passing an action 96 00:08:55,120 --> 00:08:56,740 object to this class. 97 00:09:11,560 --> 00:09:17,370 In the production code will simply inject an action that uses console pipeline. 98 00:09:17,860 --> 00:09:25,450 This is how we could do it, but for testing purposes, we'll use a mock of the action object. 99 00:09:43,220 --> 00:09:50,540 And now we can write a test that tracks that her offspring has been printed Usman at times as the number 100 00:09:50,540 --> 00:09:52,400 provided with the parameter. 101 00:10:15,480 --> 00:10:21,930 Here, I simply verify that the action mocked by this object has been called with this parameter. 102 00:10:21,960 --> 00:10:23,640 Exactly five times. 103 00:10:24,300 --> 00:10:26,310 Let's make sure this test buses. 104 00:10:28,580 --> 00:10:30,830 Great, thanks to MOCS. 105 00:10:30,920 --> 00:10:34,850 We managed to test a method which doesn't produce an output. 106 00:10:35,210 --> 00:10:42,290 Instead, we tested that a specific method was called with a given parameter and a given number of times. 107 00:10:43,010 --> 00:10:50,360 Let's summarize MOOCs are objects that can be used to substitute real dependencies for testing purposes. 108 00:10:50,840 --> 00:10:58,280 For example, we don't want to use a real database connection in unit tests instead will replace the 109 00:10:58,280 --> 00:11:05,720 object connecting to a database with a mock that provides the same interface, but returns this data. 110 00:11:06,200 --> 00:11:12,920 MOOCs are an essential part of unique testing, and it's nearly impossible to test a real life application 111 00:11:12,920 --> 00:11:13,790 without them. 112 00:11:14,300 --> 00:11:17,990 During the interview, you can be asked What is mock? 113 00:11:18,590 --> 00:11:21,800 Mock is a popular mocking library for C-sharp. 114 00:11:22,130 --> 00:11:28,070 It allows us to easily create MOOCs of interfaces, classes, Funk's or actions. 115 00:11:28,580 --> 00:11:34,850 It gives us ability to decide what result will be returned from the mocked methods, as well as validate 116 00:11:34,850 --> 00:11:36,650 if somebody has been called. 117 00:11:36,950 --> 00:11:39,890 How many times and with what parameters? 118 00:11:40,820 --> 00:11:44,840 What is the relation between mocking and dependency injection? 119 00:11:45,530 --> 00:11:52,760 The relation between them is that mocking is hard to implement without the dependency injection dependency 120 00:11:52,760 --> 00:11:59,600 injection allows us to inject some dependencies across so we can choose whether we want to inject our 121 00:11:59,600 --> 00:12:01,850 real implementations or mocks. 122 00:12:02,110 --> 00:12:08,450 If the dependency of the class would not be injected but rather created right in this class as code, 123 00:12:08,750 --> 00:12:13,880 we could not switch it to a mock implementation when we wanted to test this cross. 124 00:12:14,880 --> 00:12:15,680 All right. 125 00:12:15,970 --> 00:12:17,490 Let's eat a Botox. 126 00:12:17,790 --> 00:12:20,880 Thanks for watching, and I'll see you in the next video.