All language subtitles for pragstudio-ruby-12-testing (Transcribed on 24-Apr-2023 20-58-02)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,000 --> 00:00:07,440 Now, before we go any further, take a moment to pat yourself on the back. 2 00:00:07,440 --> 00:00:09,560 You are halfway through the Ruby course. 3 00:00:09,560 --> 00:00:10,560 Woot! 4 00:00:10,560 --> 00:00:13,360 And in the second part of this course, the game that you've been writing will get a 5 00:00:13,360 --> 00:00:15,560 lot more interesting and fun. 6 00:00:15,560 --> 00:00:18,280 But herein lies a potential problem. 7 00:00:18,280 --> 00:00:22,140 In order to figure out if we're getting the results we expect in the game, we actually 8 00:00:22,140 --> 00:00:23,300 have to run it. 9 00:00:23,300 --> 00:00:28,080 We set Larry's health to 60, we play the game, which right now is one blam and two 10 00:00:28,080 --> 00:00:30,619 roots, and then we check that Larry's health is 80. 11 00:00:30,619 --> 00:00:32,960 We have to do that math in our head. 12 00:00:32,960 --> 00:00:37,760 And as we make the game more interesting, we're also going to make it a bit more sophisticated. 13 00:00:37,760 --> 00:00:42,400 This means there'll be more to check, more math to do, more to remember to check, more 14 00:00:42,400 --> 00:00:43,839 to keep in our heads. 15 00:00:43,839 --> 00:00:48,599 So what we need is an easier way to ensure that the functionality we already have, we 16 00:00:48,599 --> 00:00:50,400 don't go and break it. 17 00:00:50,400 --> 00:00:54,239 And the best way to do that is by starting to write some unit tests now. 18 00:00:54,239 --> 00:00:57,440 Thankfully, Ruby has a strong culture of testing. 19 00:00:57,440 --> 00:01:01,440 In fact, Ruby ships with a unit testing library called Test Unit. 20 00:01:01,440 --> 00:01:06,200 Now here's the thing, all these testing libraries basically give you a way to do the same thing. 21 00:01:06,200 --> 00:01:10,440 Run parts of your program, get back some results, and then check that the results are what you 22 00:01:10,440 --> 00:01:11,560 expect. 23 00:01:11,560 --> 00:01:14,000 In this course, we'll use the RSpec library. 24 00:01:14,000 --> 00:01:18,280 It's used pervasively by both the Ruby and Rails communities, and also gives us an opportunity 25 00:01:18,280 --> 00:01:20,640 to learn how to use an external Ruby library. 26 00:01:20,640 --> 00:01:23,360 All right, now testing is a huge topic. 27 00:01:23,360 --> 00:01:25,780 In fact, it's a course all to itself. 28 00:01:25,780 --> 00:01:28,360 Our goal here is to simply get you started. 29 00:01:28,360 --> 00:01:30,840 We're not aiming for 100% test coverage. 30 00:01:30,840 --> 00:01:35,520 Yeah, specifically, we're going to look at writing tests for individual classes and methods, 31 00:01:35,520 --> 00:01:37,200 often called unit tests. 32 00:01:37,200 --> 00:01:40,400 So let's head over and get started. 33 00:01:40,400 --> 00:01:42,880 Now the first thing we need to do is install RSpec. 34 00:01:42,880 --> 00:01:45,440 Yeah, RSpec doesn't ship with Ruby. 35 00:01:45,440 --> 00:01:49,000 It's actually an external library that's distributed as a Ruby gem. 36 00:01:49,000 --> 00:01:50,000 Right. 37 00:01:50,000 --> 00:01:51,000 We just call them gems. 38 00:01:51,000 --> 00:01:52,000 Yeah, just gems. 39 00:01:52,000 --> 00:01:57,520 And gems make it easy to download, install, and use external libraries like RSpec. 40 00:01:57,520 --> 00:01:59,360 So let's get RSpec installed. 41 00:01:59,360 --> 00:02:00,360 Perfect. 42 00:02:00,360 --> 00:02:05,120 We do that by typing gem install RSpec. 43 00:02:05,120 --> 00:02:08,880 And the RSpec gem actually has dependencies on a few other gems, so we end up with four 44 00:02:08,880 --> 00:02:10,800 total gems installed. 45 00:02:10,800 --> 00:02:12,800 So now we're ready to write tests for our movie class. 46 00:02:12,800 --> 00:02:13,800 Sure. 47 00:02:13,800 --> 00:02:17,760 We'll just go back over to our TextMate file, and we're going to create a new file inside 48 00:02:17,760 --> 00:02:20,800 of here, inside of our directory here. 49 00:02:20,800 --> 00:02:26,800 I'm just going to call it movie, and the convention is to call it underscore spec dot rb. 50 00:02:26,800 --> 00:02:33,480 And at the top of this file, we want to require our movie file because we're going to be testing 51 00:02:33,480 --> 00:02:35,520 our movie class. 52 00:02:35,520 --> 00:02:39,040 And then we're going to start specing out what the movie class does. 53 00:02:39,040 --> 00:02:42,120 So the first part of that is we're going to say describe. 54 00:02:42,120 --> 00:02:45,100 Now this is part of RSpec's domain-specific language. 55 00:02:45,100 --> 00:02:46,800 So this is all Ruby code. 56 00:02:46,800 --> 00:02:48,760 It just has its own little vocabulary in here. 57 00:02:48,760 --> 00:02:53,880 We're going to say describe a movie, do, and it takes a block, so it's a do-in structure 58 00:02:53,880 --> 00:02:54,880 there. 59 00:02:54,880 --> 00:02:59,120 Then inside of the describe block, we add what are called code examples. 60 00:02:59,120 --> 00:03:03,480 We do that using it, and then we type in what we want a movie to do. 61 00:03:03,480 --> 00:03:06,160 So it has a capitalized name, for example. 62 00:03:06,160 --> 00:03:10,340 Actually, it's a capitalized title. 63 00:03:10,340 --> 00:03:11,799 And then it's got a little block structure. 64 00:03:11,799 --> 00:03:14,160 So now we can actually spec out the movie. 65 00:03:14,160 --> 00:03:18,680 So we're going to say I'm going to set up a movie, and the movie is going to be, yeah, 66 00:03:18,680 --> 00:03:20,640 Goonies with a rank of 10 is good. 67 00:03:20,640 --> 00:03:21,640 So that's the sort of setup. 68 00:03:21,640 --> 00:03:26,340 When we have a movie in that state, now we can write some expectations about it. 69 00:03:26,340 --> 00:03:31,960 So we expect that when we call movie.title, we're going to get the capitalized form of 70 00:03:31,960 --> 00:03:32,960 Goonies. 71 00:03:32,960 --> 00:03:38,060 So the way we write the expectation in RSpec is RSpec adds this method called should to 72 00:03:38,060 --> 00:03:41,120 every object as a way of making this a little bit more readable. 73 00:03:41,120 --> 00:03:45,640 So it says movie.title should, and then we use equal equal. 74 00:03:45,640 --> 00:03:50,720 That's the comparison operator, and it should equal the string capital Goonies, just like 75 00:03:50,720 --> 00:03:51,720 that. 76 00:03:51,720 --> 00:03:53,459 Now just bear in mind, this is equal equal. 77 00:03:53,459 --> 00:03:55,519 It's not a single assignment operator. 78 00:03:55,519 --> 00:03:57,279 We're actually doing a comparison there. 79 00:03:57,279 --> 00:03:58,899 Okay, then we save that file. 80 00:03:58,899 --> 00:04:00,559 We can hop back out on the command line. 81 00:04:00,559 --> 00:04:04,299 And then to run this RSpec file, we type RSpec. 82 00:04:04,299 --> 00:04:07,060 That's a command utility that was installed with RSpec. 83 00:04:07,060 --> 00:04:11,739 We give it the name of our movie spec file, moviespec.rb. 84 00:04:11,740 --> 00:04:16,900 And we see we got this one little dot at the top, and it says one example and zero failures. 85 00:04:16,900 --> 00:04:18,440 Now I like to run this with some color. 86 00:04:18,440 --> 00:04:22,920 You can pass in a dash dash color option like that, and we get a nice little happy green 87 00:04:22,920 --> 00:04:25,240 dot with our output. 88 00:04:25,240 --> 00:04:27,380 No gold stars, just green dots. 89 00:04:27,380 --> 00:04:29,240 Just green dots, yeah. 90 00:04:29,240 --> 00:04:31,820 Now I'm never confident about the first test I write. 91 00:04:31,820 --> 00:04:33,240 Is it actually testing something? 92 00:04:33,240 --> 00:04:35,800 So let's come back over here and let's make it fail. 93 00:04:35,800 --> 00:04:40,200 If I go into the movie file, and let's just change this. 94 00:04:40,200 --> 00:04:44,400 Let's say we forgot to capitalize the title, so I'm just gonna comment that out, bad programmer. 95 00:04:44,400 --> 00:04:47,280 And we come back out to the command line, we run it again. 96 00:04:47,280 --> 00:04:51,120 Oh, now we get some red failing messages here. 97 00:04:51,120 --> 00:04:53,640 And we see movie title should equal Goonies. 98 00:04:53,640 --> 00:04:58,919 It says expected uppercase Goonies got Goonies using that equal operator. 99 00:04:58,919 --> 00:05:00,360 So we are actually testing something. 100 00:05:00,360 --> 00:05:05,640 We can go back into our movie and put the capitalize back on there, come back out to 101 00:05:05,640 --> 00:05:10,039 the command line again, and now we've got it all back to green. 102 00:05:10,040 --> 00:05:12,240 Let's also show them how to run it in Textmate. 103 00:05:12,240 --> 00:05:18,000 Sure, over in Textmate, if we open the movie spec.rb file, we can just run it inside of 104 00:05:18,000 --> 00:05:20,440 Textmate because of the RSpec bundle here. 105 00:05:20,440 --> 00:05:23,400 And if I run it, I get the same green color coding. 106 00:05:23,400 --> 00:05:26,680 And it actually says movie has a capitalized name. 107 00:05:26,680 --> 00:05:29,320 So it printed out that string that we had in the it block. 108 00:05:29,320 --> 00:05:31,400 We get a green bar instead of a green dot. 109 00:05:31,400 --> 00:05:32,400 There you go. 110 00:05:32,400 --> 00:05:33,400 Yeah. 111 00:05:33,400 --> 00:05:34,660 Now let's write another test. 112 00:05:34,660 --> 00:05:36,160 Let's test the initial rank. 113 00:05:36,160 --> 00:05:38,820 Sure, we'll just have another code example for that. 114 00:05:38,820 --> 00:05:44,219 So it starts with it, we'll just say has an initial rank. 115 00:05:44,219 --> 00:05:47,480 And then inside of that, well, we're going to need another movie object, and I'm going 116 00:05:47,480 --> 00:05:49,560 to do some copy paste coding for a while. 117 00:05:49,560 --> 00:05:51,760 We'll clean this up a little bit later. 118 00:05:51,760 --> 00:05:52,760 So there's our movie. 119 00:05:52,760 --> 00:05:55,480 Each of these it blocks runs on their own. 120 00:05:55,480 --> 00:05:59,480 So we can't access inside of this it block, we can't access this movie. 121 00:05:59,480 --> 00:06:02,280 So we've got to set up a new movie object here. 122 00:06:02,280 --> 00:06:04,420 And I'm going to say movie.rank. 123 00:06:04,420 --> 00:06:08,980 It should equal 10 because that's the value that we actually passed in. 124 00:06:08,980 --> 00:06:11,840 So I'm going to go ahead and run this inside of TextMe. 125 00:06:11,840 --> 00:06:15,680 And sure enough, we've got two green bars here. 126 00:06:15,680 --> 00:06:19,680 You know, as you were writing those, I found myself thinking about the other code examples 127 00:06:19,680 --> 00:06:20,680 I want. 128 00:06:20,680 --> 00:06:24,200 Like I want to test the 2S and the thumbs up and the thumbs down. 129 00:06:24,200 --> 00:06:27,120 And I kind of don't want to forget about the tests that I want. 130 00:06:27,120 --> 00:06:29,560 So maybe we could create a list of examples. 131 00:06:29,560 --> 00:06:30,560 Sure. 132 00:06:30,560 --> 00:06:32,160 Then we can just cross them off one by one. 133 00:06:32,160 --> 00:06:33,160 Right. 134 00:06:33,160 --> 00:06:35,240 So what's the next code example we want to write here? 135 00:06:35,240 --> 00:06:36,760 Well, let's see. 136 00:06:36,760 --> 00:06:38,320 Movie has a 2S. 137 00:06:38,320 --> 00:06:39,440 So let's test that. 138 00:06:39,440 --> 00:06:41,320 That's a string representation. 139 00:06:41,320 --> 00:06:43,080 So let's just add that to the list here. 140 00:06:43,080 --> 00:06:49,120 We can just use an it block and then we'll say has a string representation. 141 00:06:49,120 --> 00:06:55,120 And if we don't give it a do block, what happens and we run this now, we get a pending test. 142 00:06:55,120 --> 00:06:58,640 So you see it turned it yellow and the yellow one is has a string representation. 143 00:06:58,640 --> 00:07:00,840 It says pending, not yet implemented. 144 00:07:00,840 --> 00:07:04,719 Back out on the command line, if we run it with coloration, we get some yellow colors 145 00:07:04,719 --> 00:07:07,539 there and we also get this pending marker. 146 00:07:07,539 --> 00:07:08,880 Movie has a string representation. 147 00:07:08,880 --> 00:07:11,500 So we've got a little to do list started here. 148 00:07:11,500 --> 00:07:15,840 So we also want to test that we increase the rank because we have this thumbs up method. 149 00:07:15,840 --> 00:07:25,840 So let's write a test that says it increases rank by one when given a thumbs up. 150 00:07:25,840 --> 00:07:28,880 From your mouth to the specs itself. 151 00:07:28,880 --> 00:07:30,659 And then we have our thumbs down method. 152 00:07:30,659 --> 00:07:32,120 So we should test that too. 153 00:07:32,120 --> 00:07:34,920 It decreases rank by one. 154 00:07:34,920 --> 00:07:40,920 Decreases rank by one when given a thumbs down. 155 00:07:40,920 --> 00:07:42,340 All right. 156 00:07:42,340 --> 00:07:45,880 So we've got our list of three specs that we want to write. 157 00:07:45,880 --> 00:07:47,540 So let's start knocking these off the list. 158 00:07:47,540 --> 00:07:49,640 We have the first one, string representation. 159 00:07:49,640 --> 00:07:51,040 We need a do block there. 160 00:07:51,040 --> 00:07:52,040 All right. 161 00:07:52,040 --> 00:07:53,380 We're going to need a movie for that. 162 00:07:53,380 --> 00:07:54,380 Just like everything else. 163 00:07:54,380 --> 00:07:55,719 We need a movie. 164 00:07:55,719 --> 00:07:57,240 Goonies or rank a 10. 165 00:07:57,240 --> 00:07:58,240 That's fine. 166 00:07:58,240 --> 00:08:06,960 But our expectation is when we call movie 2S, it should return a string that says Goonies 167 00:08:06,960 --> 00:08:09,840 has a rank of 10. 168 00:08:09,840 --> 00:08:12,520 I think there's a period at the end, but the tests are going to tell us. 169 00:08:12,520 --> 00:08:14,520 So if I run that. 170 00:08:14,520 --> 00:08:15,520 No. 171 00:08:15,520 --> 00:08:20,640 Goonies has a rank of 10 was expected with a period, but we don't have a period in our 172 00:08:20,640 --> 00:08:21,640 2S method. 173 00:08:21,640 --> 00:08:22,640 So let's take that off. 174 00:08:22,640 --> 00:08:24,240 Run it again. 175 00:08:24,240 --> 00:08:25,240 Okay. 176 00:08:25,240 --> 00:08:27,200 Now we've got that one all fixed up. 177 00:08:27,200 --> 00:08:29,520 So let's tackle the other one down here. 178 00:08:29,520 --> 00:08:32,159 Increases rank by one when given a thumbs up. 179 00:08:32,159 --> 00:08:34,240 It should actually be increases. 180 00:08:34,240 --> 00:08:35,960 We'll fix the documentation there. 181 00:08:35,960 --> 00:08:36,960 All right. 182 00:08:36,960 --> 00:08:38,600 So I'm going to do this one a little bit different. 183 00:08:38,600 --> 00:08:41,640 What we want to have happen is we want an initial rank set. 184 00:08:41,640 --> 00:08:42,640 We want to create a movie. 185 00:08:42,640 --> 00:08:43,919 We want to thumbs up it. 186 00:08:43,919 --> 00:08:46,480 And then we want to compare that initial rank. 187 00:08:46,480 --> 00:08:49,140 We want to compare the new rank to its initial rank. 188 00:08:49,140 --> 00:08:51,720 So to do that, I'm going to create an initial rank variable. 189 00:08:51,720 --> 00:08:53,880 I'm going to set it to 10. 190 00:08:53,880 --> 00:08:56,439 And then we'll create our movie object. 191 00:08:56,440 --> 00:08:57,440 We'll shortcut there. 192 00:08:57,440 --> 00:08:58,960 We'll leave it at Goonies. 193 00:08:58,960 --> 00:09:02,400 And then we'll just pass in the initial rank like that. 194 00:09:02,400 --> 00:09:06,000 Then our expectation is, or actually then we have to do something to the object. 195 00:09:06,000 --> 00:09:09,080 So we call movie.thumbsup. 196 00:09:09,080 --> 00:09:14,840 Then our expectation is when we look at the rank of that movie, it should equal the initial 197 00:09:14,840 --> 00:09:17,640 rank plus one. 198 00:09:17,640 --> 00:09:19,880 So it should have increased the rank by one. 199 00:09:19,880 --> 00:09:20,880 Yeah? 200 00:09:20,880 --> 00:09:21,880 Yep. 201 00:09:21,880 --> 00:09:22,880 Run that. 202 00:09:22,880 --> 00:09:24,600 We've got one more crossed off the list. 203 00:09:24,600 --> 00:09:25,940 We're on a roll. 204 00:09:25,940 --> 00:09:29,000 So this last one is very similar to the one before. 205 00:09:29,000 --> 00:09:32,080 I just need a little space there. 206 00:09:32,080 --> 00:09:34,800 Let me just take this. 207 00:09:34,800 --> 00:09:37,000 We're going to clean this up in a few minutes. 208 00:09:37,000 --> 00:09:39,280 In this case, we're going to decrease the rank by one. 209 00:09:39,280 --> 00:09:41,660 So we're going to call thumbs down here. 210 00:09:41,660 --> 00:09:45,380 And the initial rank should decrease by one, just like that. 211 00:09:45,380 --> 00:09:46,680 How do we do? 212 00:09:46,680 --> 00:09:49,320 Green across the board. 213 00:09:49,320 --> 00:09:51,520 So now we have specs for our movie behavior. 214 00:09:51,520 --> 00:09:54,480 And if we break something going forward, we'll know about it. 215 00:09:54,480 --> 00:09:58,320 Yeah, I love the confidence that that gives us as we continue to code. 216 00:09:58,320 --> 00:10:02,080 But we do need to do one thing before we go too much further, and that's clean up all 217 00:10:02,080 --> 00:10:03,720 this duplication in the tests. 218 00:10:03,720 --> 00:10:05,160 So let's do that. 219 00:10:05,160 --> 00:10:06,720 So let's have a look at this duplication. 220 00:10:06,720 --> 00:10:08,760 We've got a couple lines here. 221 00:10:08,760 --> 00:10:12,200 Those are also repeated in this little spec here. 222 00:10:12,200 --> 00:10:14,880 And then in each of these above, we're creating a new movie. 223 00:10:14,880 --> 00:10:16,800 Clearly, we don't want to be doing that. 224 00:10:16,800 --> 00:10:19,400 So let's just take one of them from down below. 225 00:10:19,400 --> 00:10:22,360 I'm going to take these two lines and just copy them. 226 00:10:22,360 --> 00:10:24,520 And we want to do this in one spot in our code. 227 00:10:24,520 --> 00:10:27,780 And in our spec, we can do that in what's called a before block. 228 00:10:27,780 --> 00:10:30,040 It takes a block structure like that. 229 00:10:30,040 --> 00:10:33,240 I can just paste that code in there. 230 00:10:33,240 --> 00:10:39,400 And this code is going to run before every it block, before every code example here. 231 00:10:39,400 --> 00:10:44,700 Now we need to use instance variables for these so that they're accessible down in those 232 00:10:44,700 --> 00:10:45,700 code examples. 233 00:10:45,700 --> 00:10:49,040 So we've got instance variables for initial rank and for movie. 234 00:10:49,040 --> 00:10:52,760 Now down inside of the code examples, we can remove this line because it's going to be 235 00:10:52,760 --> 00:10:54,319 set up in the before block. 236 00:10:54,319 --> 00:10:56,719 We just have to change this to an instance variable. 237 00:10:56,719 --> 00:11:02,360 The same way, we can remove this line here, change it to an instance variable, change 238 00:11:02,360 --> 00:11:03,360 this one. 239 00:11:03,360 --> 00:11:04,500 So we're just refactoring some code here. 240 00:11:04,500 --> 00:11:06,140 That one's OK. 241 00:11:06,140 --> 00:11:08,579 This one, we've got initial rank and movie already set up. 242 00:11:08,579 --> 00:11:13,459 So we'll change that to an instance variable, that one as well, and initial rank. 243 00:11:13,459 --> 00:11:15,560 Same thing here. 244 00:11:15,560 --> 00:11:18,199 Just cleaning these up nicely a little bit. 245 00:11:18,200 --> 00:11:19,200 All right? 246 00:11:19,200 --> 00:11:22,440 So we've got all that extracted up in the before block. 247 00:11:22,440 --> 00:11:27,100 Now if we run our tests, hopefully they all pass, and they do, so our refactoring there 248 00:11:27,100 --> 00:11:28,520 was successful. 249 00:11:28,520 --> 00:11:32,120 And we've removed all the duplication, which is kind of nice. 250 00:11:32,120 --> 00:11:34,760 You know, we forgot to add one test. 251 00:11:34,760 --> 00:11:35,760 Uh-oh. 252 00:11:35,760 --> 00:11:38,560 A movie can have a default value for its rank. 253 00:11:38,560 --> 00:11:44,120 Well, that gives us a good opportunity to look at another feature of our spec, context. 254 00:11:44,120 --> 00:11:49,560 A context gives us a way to organize examples with similar setup within a spec file. 255 00:11:49,560 --> 00:11:52,120 And you're talking about something having a default rank. 256 00:11:52,120 --> 00:11:55,920 So we kind of have a movie that's set up a little bit differently than the one we have 257 00:11:55,920 --> 00:11:58,080 up in our before block now. 258 00:11:58,080 --> 00:11:59,400 So let's create a context for that. 259 00:11:59,400 --> 00:12:01,920 I'm going to give myself a lot of space here. 260 00:12:01,920 --> 00:12:08,640 Context, we can give it a name, created with a default rank. 261 00:12:08,640 --> 00:12:11,040 So this is a movie created with a default rank. 262 00:12:11,040 --> 00:12:12,720 It's a block structure. 263 00:12:12,720 --> 00:12:16,120 Inside of here, we can have another before block. 264 00:12:16,120 --> 00:12:20,280 And inside of that before block, we'll create a movie fitting this context. 265 00:12:20,280 --> 00:12:22,440 So it's going to be a movie. 266 00:12:22,440 --> 00:12:24,360 It's going to be a new movie. 267 00:12:24,360 --> 00:12:25,600 And it's just going to be Goonies. 268 00:12:25,600 --> 00:12:28,760 We're not going to pass in an explicit rank here. 269 00:12:28,760 --> 00:12:29,760 Right? 270 00:12:29,760 --> 00:12:33,100 So for that context, then we can start to write an example. 271 00:12:33,100 --> 00:12:38,120 In this case, it's going to be has a rank of zero. 272 00:12:38,120 --> 00:12:40,940 And then inside of that, we can put the expectation. 273 00:12:40,940 --> 00:12:47,760 So the movie, movie.rank should equal zero because we know the default rank is zero. 274 00:12:47,760 --> 00:12:51,660 So we've got a context here that does a couple things. 275 00:12:51,660 --> 00:12:55,260 It sets up some common code to demonstrate that context. 276 00:12:55,260 --> 00:12:59,120 And then we make assertions or expectations against that code to make sure it's doing 277 00:12:59,120 --> 00:13:00,900 what we expect it to do. 278 00:13:00,900 --> 00:13:02,480 So let's go ahead and run that. 279 00:13:02,480 --> 00:13:07,000 And you notice when I run in TextMate, I've got all the regular code examples, but then 280 00:13:07,000 --> 00:13:12,660 nested down in that is a movie created with a default rank has a rank of zero. 281 00:13:12,660 --> 00:13:16,220 It kind of reads like you would write something like a software spec. 282 00:13:16,220 --> 00:13:20,640 If we look at that on the command line, you notice we don't get it with the default color 283 00:13:20,640 --> 00:13:27,640 option, but if we pass in the dash dash format and dock option, it'll neatly nest these. 284 00:13:27,640 --> 00:13:32,200 We see movie created with a default rank has a rank of zero. 285 00:13:32,200 --> 00:13:34,540 Remember, this is just a start. 286 00:13:34,540 --> 00:13:38,400 The test may seem trivial at this point, but it's critical that we get a good foundation 287 00:13:38,400 --> 00:13:41,579 of tests in place before we start to add more functionality. 288 00:13:41,579 --> 00:13:42,660 That's a really good point. 289 00:13:42,660 --> 00:13:46,360 So in the exercise, you're going to have a chance to add some tests to your game. 290 00:13:46,360 --> 00:13:49,260 And then when in the next section, we're going to look at conditionals. 291 00:13:49,260 --> 00:13:53,260 This means we'll be able to randomly woot or blam our players, which means the outcome 292 00:13:53,260 --> 00:13:55,740 in the game won't be the same every time. 293 00:13:55,740 --> 00:13:56,740 Happy spec-ing! 294 00:13:56,740 --> 00:14:01,740 We're going to look at conditionals so that we can randomly woot or blam your players. 295 00:14:01,740 --> 00:14:04,740 Randomly woot and blam! 296 00:14:04,740 --> 00:14:05,740 Okay. 297 00:14:05,740 --> 00:14:06,740 All right. 298 00:14:06,740 --> 00:14:21,180 So take a few minutes to put some tests in your game. 299 00:14:21,180 --> 00:14:32,300 The next section, we'll look at how to use conditionals. 24980

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.