[MUSIC] hello, and welcome to this jumpcast about checking out in Ruby. In this video we’re going to appear at TestDriven progress, or TDD for brief. That is the approach of writing your testfirst after which creating the code, so that the testpassed later. We will write some specs usingMiniTest, which is a lightweight testing suite that has been included in thestandard Ruby library when you consider that version 1.9. After we’ve written the exams, we’re goingto write some code and try to get the entire assessments to go. The app that we are going to build isbased on FizzBuzz. Now, if you haven’t come throughout FizzBuzzbefore, it can be a technique of counting the place multiples of three arereplaced via Fizz.And multiples of 5 are changed by using Buzz,as you will discover right here. We will put into effect this by way of creatinga FizzBuzz category that has a approach called print, as we cansee right here. An integer is entered as an argument and astring is lower back akin to what it wouldbe in FizzBuzz. So, the integer 15 would become FizzBuzzbecause it can be a more than one of 3 and 5. So as you can find, the variable fb isholding an example of the FizzBuzz type. And we can use this to peer how the printmethod works.A good way to do fb.Print three and it displaysFizz because that is a a couple of of three. We do fb.Print 5, we get Buzz. Let’s try 15, and we get FizzBuzz cuz it’sa more than one of each. Now, if we try a number that’s no longer amultiple of three or 5, akin to 13, we just get that numberreturned as a string. Yet another cool factor that we will do is entera range of values. So for illustration, i will be able to enter print, 1 to 15, and it is going to return and array of allthe specific FizzBuzz values. So, that is how the class will have to work inthe end.Let’s take a couple of steps again now and appear at writing the experiment to explain thatbehavior. We’d like two records, so I’ve got this folderhere called Fizzbuzz, if I open it up, we can see thetwo records. One for the code, which I’ve calledfizzbuzz.Rb, and one for the specs, which I’ve calledspec.Rb. Because we’re making use of experiment pushed development,we are going to with a scan. And if I open this up, we will see our testfile here. On the begin of a scan file, we’d like torequire minitest/autorun. We additionally ought to require the file thatcontains our code. Although it can be empty at the moment, westill ought to require it to start with. I’ve used require_relative here and thename of the file, FizzBuzz.And this works similar to the normalrequire, however as an alternative of utilizing Ruby’s load route, it loads from a pathrelative to the file itself. Now, seeing that both of these files are in thesame folder then I with no trouble have got to cross the file title as an argument,as you’ll find here. The excellent thing about specs is that theyread very much like English. Specs contain descriptions of behaviorthat we require. Now, considering that that is best a small type, we only have one description here, calledFizzBuzz. However for greater purposes, you could writelots of different descriptions for differentparts of the appliance.Now, a description begins with a beforeblock that one can find, here. This is run before all the specs, so weuse it to setup whatever that we have to usein the specs. In this case, we need to setup an instancemethod referred to as, fb, here. And this is just a new example of the FizzBuzz class that we’re going tocreate later. And we also have an illustration method callednumber. Now, this is a randomly generated numberthat’s surely now not a a couple of of 15. I do this by using multiplying a random numberfrom between 1 to a hundred by using 15, so as to make it a surely a multipleof 15 and then adding 1, in order to warranty that 3, 5 and 15 are notdivisors of this number. Next, we write the genuine scan. MiniTest makes use of a type of methods thatuse the phrases ought to and is not going to to describe the expectedbehavior that we would like. Now, each spec is written inside an itblock that described what is being confirmed. You can find an illustration here in our firsttest. We comfortably write the keyword, it, followed via an outline of the conduct weexpect.So the first behavior I need to experiment, isthat we return Fizz, for multiples of 3. And that is adopted by a block. Starts with do, and ends with the keywordend. And inside this, we use the methodmust_equal. As I mentioned earlier, the entire methodsare either need to or won’t. We will see an example of will not later. So what we’re saying right here is, if we usethe print process with our fb example variable, which rememberis an instance of our FizzBuzz class.And we use a number that’s a more than one of3. I’ve created that with the aid of multiplying ournumber instance variable by way of 3 to guarantee that it’s a multiple of three. Then this need to equal Fizz. So the return value of this system must beFizz. Lots of the assessments are written in a similarway. So, we will see the following scan is that itreturns Buzz for multiples of 5. And this is very similar except theargument I used for the print system is 5 occasions the quantity, which willguarantee that it is a a couple of of 5. And we used the must_equal method againand say it need to be equal to the string, Buzz.And in a equivalent method, my next scan istesting to look that we return FizzBuzz formultiples of 15. And if we just multiply through 15, the number,to warranty we have a a couple of of 15 and we say that need to equalthe string FizzBuzz. Our next experiment is a scan to claim that itreturns the number as a string for all non FizzBuzz numbers, so something that is not amultiple of 3, 5, or 15. Now bear in mind, we guarantee that thisinstance variable quantity was once no longer a more than one of 15, so accordingly three, 5, and 15,each will not divide into that quantity.And this time it have got to equal the numberitself as a string. So we use the two string approach to testthat these two things are the same. The return worth of the print process isgiving the numbers a string. Our next scan is utilizing one of the vital won’tmethods and we do not technically want this testbecause we have already established the whole lot. Now we have demonstrated this rather above but this isjust an example of how you would use one of thewon’t methods. And that is to assert, it doesn’t sayFizzBuzz when it mustn’t do. So right here, when we have a quantity that’s now not a a couple of of 15, recall that numberdefinitely is not. Then we say it will not equal FizzBuzz. Now we have truely in the test rightabove simply tested that this equals the number asa string.So like I stated, technically, we don’tactually ought to test that, but it surely’s useful just to demonstratehow to use will not. In fact, there’s more commonly, a received’tequivalent for the entire need to methods. So we’ve got must_equal and in addition wont_equal. And we are going to see some more examples of themust methods now. The next factor is, we would like it to come back an array object after we give a range as anargument. And it is a just right instance of yet another mustmethod right here, the have got to be an instance of. And, here we’re saying that the returnvalue after we enter a variety as an argument into theprint method, have got to be an array. And this here may also be any form of Rubyobject. On this case it is an array that we wantreturned. So, we’re simply testing that an array comesback. And, the final three exams are all aboutsome error testing that I want to do.So, we simply want to handle a number of differenterrors that might happen. FizzBuzz is simplest relatively for integers, sowe want it to raise an error if the number isnegative. So, after we use this system, that is themust_raise system. Once more, there’s a will not raise similar aswell. And we pop the print process right here with a negative argument and we’re pronouncing thatthis wants to elevate an error and actuallydefining as an argument error that needs to beraised. We have now obtained a couple of extra errors that i would like tobe raised here. So, the subsequent one is that if we enter zerowe need it to also lift an error. That is very an identical we use a proc and we put zero’s the argument into the printmethod. And we are saying this must_raise an argumenterror again. And one final error we wish to scan for is that if we don’t use integers in theargument. So, we’re announcing again, must_raise anerror if it’s now not an integer. So, we use a proc once more with the printmethod, with an argument of two.5, so obviously now not an integer and thismust_raise an argument error.So, there are all our exams. That’s what we anticipate from ourapplication. So, the first thing we do is clearly runthe spec file and spot what occurs. So, if i’m going back into my console here andexit from IRB. Now, i am in the folder direction where my filesare saved. And spec.Rb is just a Ruby file, so we runit by using with no trouble coming into ruby spec.Rb. And after I press enter, it’ll run thetests and we are able to see the output on the bottom, there werenine exams that we wrote. And it is honestly lower back 9 mistakes. Which you can truly return and investigate on these errors, but they may be quite verbose inthis case.And generally, that is for the reason that we don’tactually have any code. If we go and have a look at ourFizzBuzz.Rb file right here, it’s wholly empty, so no surprise it’sgiving us all those errors. So, let’s have a go at hanging togetherthe FizzBuzz category. This is my first attempt right here. Just save that. Simply quickly run through this. I have the print approach here and webasically run through the tests to peer if the numbers aremultiple of 15. So i’m doing this utilizing this moduloarithmetic operator here which in actual fact exams the remainder whenyou divide with the aid of 15. Now if the quantity is a a couple of of 15 thenthe remainder might be zero.So this means that if we get zero, it is amultiple of 15 so we wish to return, FizzBuzz, else if it’s a numberof, if it’s a multiple of 3 then we return, Fizz, and if it’s amultiple of 5, we return, Buzz. Otherwise, we just need to return thenumber, converted to a string. So, let’s run our checks once more and see howwe do. If we return into the terminal and typein ruby spec.Rb, we will see now that we have now run 9nine assessments,eight assertions. But we got three disasters and one error. So, if we simply return and have a look atthese, here, the first failure was once, it raises an error if thenumber is zero. It failed to lift an error if the quantity waszero and we’ve not quite acquired any errorhandling in here. So, i’ll must look at that quickly.Here we have now obtained an error. The experiment used to be it returns an array objectwhen a variety is given as an argument, as you’ll find right here,so that is not working. We’ve not applied but this is becausethere’s no process, the percent signal, the modulo operator would not existfor a variety, so that resulted in error. So we need to go forward and fix that. The other two failures were the errorsbeing raised, so like I said, we have not received any errorhandling. So, let’s repair the error first with therange. Let’s try to deal with that. So, if we go back to our FizzBuzz file Ithink we need one other process here. So, i will exchange the print methodto this approach here. And what I need to do now’s simply investigate ifthe argument responds to the every approach. So that is truly the is it an array oris it a variety? And if that is so, then I need to iterate overeach number in that range or array.And use a system known as experiment, which isactually going to be my historic print method. So i will replace that down here tobe referred to as scan now. And that i most effective want this to be obtainable fromwithin this classification, so i’m going to without a doubt make it a privatemethod as well. So, this print approach here will haveaccess to this procedure known as test. And, here we say, so if the argumententered responds to every then we’ll iterate through each and every price and we’ll runthis scan on it and send back the output. In any other case, then we will simply run the methodtest on it, just like we did earlier than. So, this must have the performance fortesting ranges of numbers.So, let’s store that and go back and runour tests. Once more, ruby spec.Rb, in the terminal. And now we can see that every one nine checks andassertions were run. We don’t have any blunders, which is just right but we’vestill obtained three disasters and if I simply appear again now we can see, you can seeit says walking the tests right here. You have bought that every dot is a experiment beingpassed, and now we have acquired fail, fail, fail. And every one is to do with our errorhandling. So it appears zero terrible numbers, and non integer values, can still beentered. So, that is our next thing to fix, go backto my FizzBuzz file. And, i am simply really in to maintain thisin the experiment system right here.And by using fairly including these strains here thatraises an argument error until the quantity isgreater than zero. So this will have to maintain numbers that arezero or beneath. So negative numbers or zero, that shoulddeal with two of our exams. And this line right here raises an error andgives the suggestions integers handiest until the quantity is aninteger. I’m utilizing the is_a approach there to test whether or not the argument entered is in fact aninteger. So, that will have to raise the error that wewant. So give some feedback to individuals about the arguments they are entering and why it’sraising an error. If I simply keep that now and run the exams. Ruby spec.Rb, and press Enter. We are able to see that it is strolling the tests andall these dots point out that all the checks have passed, as we cansee via the summary, right here.We’ve got got nine tests, nine assertions, andno failures and no error. That’s pleasant. We know have some functioning code. And what now we have carried out is we now have started offwith our checks written our tests and described using specs, what we wish tohappen, the habits, what we wish in our code. After which, we’ve got gone forward and written ourcode and we noticed it when in phases. We had just a few mistakes and disasters to startwith, however we progressively delivered to the code andeventually every experiment passed.I’m hoping you observed that useful. And make certain each time you’re writing yourRuby code to any extent further, you write the exams first and thenwrite the code. Thanks for listening. [MUSIC].