1 00:00:00,090 --> 00:00:04,510 In this lecture, we are going to write our first test to get started. 2 00:00:04,530 --> 00:00:08,970 We will update the test file for the app component in your editor. 3 00:00:08,970 --> 00:00:11,550 Search for this file in our project. 4 00:00:13,730 --> 00:00:19,070 Let's rename the file by changing the extension from spec broken to spec. 5 00:00:21,360 --> 00:00:24,420 Next, let's clear the contents of the file. 6 00:00:24,450 --> 00:00:28,320 The first test will be writing is called a sanity test. 7 00:00:28,320 --> 00:00:34,590 Similar to the development process of an application, you are bound to encounter errors while writing 8 00:00:34,590 --> 00:00:35,370 tests. 9 00:00:35,370 --> 00:00:39,150 You're going to spend time figuring out why a test isn't working. 10 00:00:39,150 --> 00:00:44,010 In some cases, a test may not fail because there's anything wrong with your code. 11 00:00:44,010 --> 00:00:47,160 The cause of a failure can come from your tool set. 12 00:00:47,340 --> 00:00:49,500 It's common for tools to fail. 13 00:00:49,500 --> 00:00:53,910 If a tool fails, we can safely assume there's nothing wrong with our code. 14 00:00:53,910 --> 00:00:58,260 That brings us to the question how do we verify a tool fails? 15 00:00:58,260 --> 00:01:01,920 The most common solution is to write a sanity test. 16 00:01:01,950 --> 00:01:05,250 A sanity test is a test that always passes. 17 00:01:05,250 --> 00:01:10,230 If a sanity test fails, we can assume there's something wrong with our tools. 18 00:01:10,230 --> 00:01:14,880 I highly recommend writing a sanity test as your first test. 19 00:01:15,060 --> 00:01:22,380 We're going to write a sanity test inside the app test file first at a function called Describe. 20 00:01:24,700 --> 00:01:28,720 The described function is defined by the Jasmyn Library. 21 00:01:28,750 --> 00:01:32,620 It's not uncommon for an application to have hundreds of tests. 22 00:01:32,650 --> 00:01:35,590 Organizing your tests is very important. 23 00:01:35,630 --> 00:01:38,020 It'll help you debug your application. 24 00:01:38,050 --> 00:01:44,920 The described function allows us to create a test suite, which is another way of saying a group of 25 00:01:44,920 --> 00:01:45,790 tests. 26 00:01:45,940 --> 00:01:48,370 This function has two arguments. 27 00:01:48,370 --> 00:01:51,940 The first argument is a description of the test suite. 28 00:01:51,970 --> 00:01:55,370 Typically, developers will write the name of the component. 29 00:01:55,390 --> 00:01:58,690 Let's set this argument to app component. 30 00:02:00,850 --> 00:02:05,080 The second argument is a function let's pass in an arrow function. 31 00:02:07,210 --> 00:02:10,880 Inside this function, we can begin defining our tests. 32 00:02:10,900 --> 00:02:13,330 Let's run a function called it. 33 00:02:15,600 --> 00:02:16,530 Immediately. 34 00:02:16,530 --> 00:02:20,020 You'll notice we're not importing these functions before using them. 35 00:02:20,040 --> 00:02:23,220 We're running our code through Jasmine by default. 36 00:02:23,250 --> 00:02:26,850 Jasmine will define a set of global functions. 37 00:02:27,030 --> 00:02:30,190 The IT function is predefined for us. 38 00:02:30,210 --> 00:02:33,450 We don't have to do anything to make sure it's available. 39 00:02:33,600 --> 00:02:37,100 The IT function allows us to write a single test. 40 00:02:37,110 --> 00:02:40,710 Initially, the name of the function might sound strange. 41 00:02:40,710 --> 00:02:46,350 However, throughout this section you're going to quickly discover that tests are verbose. 42 00:02:46,380 --> 00:02:49,140 Keep following along and you'll see what I mean. 43 00:02:49,320 --> 00:02:51,750 This function has two arguments. 44 00:02:51,750 --> 00:02:55,600 The first argument should describe what the test is performing. 45 00:02:55,620 --> 00:02:59,330 Jasmine and Karma will generate a test report. 46 00:02:59,340 --> 00:03:02,640 The value of this argument will appear in the report. 47 00:03:02,670 --> 00:03:05,730 The identifier should be short and concise. 48 00:03:05,730 --> 00:03:09,990 Let's type the following should pass sanity test. 49 00:03:12,150 --> 00:03:15,570 As I mentioned earlier, tests should be descriptive. 50 00:03:15,600 --> 00:03:19,260 Typically, testing libraries will name their functions. 51 00:03:19,260 --> 00:03:21,890 To help you describe the purpose of a test. 52 00:03:21,900 --> 00:03:28,800 For example, we can read this line of code like so it should pass sanity test. 53 00:03:29,460 --> 00:03:31,980 The second argument is a function that will run. 54 00:03:31,980 --> 00:03:35,880 When we test our application, we will pass in an arrow function. 55 00:03:38,120 --> 00:03:41,120 We're going to create what's called an assertion. 56 00:03:43,240 --> 00:03:49,780 In the software development world, an assertion is an expression that evaluates to either true or false. 57 00:03:49,810 --> 00:03:51,400 It's as simple as that. 58 00:03:51,430 --> 00:03:56,050 If the assertion we create evaluates to false, the test fails. 59 00:03:56,050 --> 00:03:58,490 If the assertion evaluates to true. 60 00:03:58,510 --> 00:03:59,950 The test passes. 61 00:04:02,090 --> 00:04:07,070 We're going to create an assertion that checks if true is equal to true. 62 00:04:07,100 --> 00:04:13,900 We can use a comparison operator to create this expression, but Jasmine has an even better solution 63 00:04:13,910 --> 00:04:15,320 inside the function. 64 00:04:15,320 --> 00:04:16,470 Add the following. 65 00:04:16,490 --> 00:04:18,790 Expect true to be true. 66 00:04:18,800 --> 00:04:19,279 Thi. 67 00:04:21,339 --> 00:04:26,570 We're using another global function defined by Jasmine called expect. 68 00:04:26,590 --> 00:04:31,020 This function will return an object with functions that are called matters. 69 00:04:31,030 --> 00:04:36,220 In the resource section of this lecture, I provide a link to a list of functions. 70 00:04:38,330 --> 00:04:41,220 The expect function takes in a value. 71 00:04:41,270 --> 00:04:43,280 It can be any type of value. 72 00:04:43,310 --> 00:04:49,730 It will return an object with methods you can use to perform a test on the value you passed in. 73 00:04:49,760 --> 00:04:52,820 These functions are referred to as matters. 74 00:04:52,850 --> 00:04:55,490 The purpose of these functions is to perform. 75 00:04:55,490 --> 00:05:01,890 A test on the value passed in this page provides a comprehensive list of available matters. 76 00:05:01,910 --> 00:05:05,680 One of the measures we're using is called to be truth. 77 00:05:05,830 --> 00:05:07,190 The to be truth. 78 00:05:07,190 --> 00:05:09,650 You matcher can check a boolean value. 79 00:05:09,680 --> 00:05:12,140 The Boolean value must be true. 80 00:05:12,170 --> 00:05:14,540 Let's head back to our editors. 81 00:05:14,570 --> 00:05:17,260 The tests we've written will always pass. 82 00:05:17,270 --> 00:05:19,520 That's the goal of a sanity test. 83 00:05:19,550 --> 00:05:25,510 We're creating a test that will always pass to verify our tools are working as intended. 84 00:05:25,520 --> 00:05:31,590 If the test doesn't pass, we'll know that there's a problem with the tools and not our tests. 85 00:05:31,610 --> 00:05:33,740 Let's switch over to the browser. 86 00:05:36,010 --> 00:05:40,630 If the test does not show up, you may need to restart the command. 87 00:05:40,660 --> 00:05:43,600 Jasmine has generated a test report. 88 00:05:43,630 --> 00:05:46,580 It's a list of tests that have been executed. 89 00:05:46,600 --> 00:05:49,990 In this list, the sanity test has passed. 90 00:05:50,020 --> 00:05:53,350 This response indicates that the test passed. 91 00:05:53,350 --> 00:05:57,340 We can begin to write more tests in the next set of lectures. 92 00:05:57,340 --> 00:06:00,430 Let's try writing more practical tests.