|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +These exercises lean on Test-Driven Development (TDD), but they're not an |
| 4 | +exact match. |
| 5 | + |
| 6 | +The following steps assume that you are in the same directory as the test |
| 7 | +suite. |
| 8 | + |
| 9 | +You must have the `minitest` gem installed: |
| 10 | + |
| 11 | + $ gem install minitest |
| 12 | + |
| 13 | +## Step 1 |
| 14 | + |
| 15 | +Run the test suite. It's written using the Minitest framework, and can be |
| 16 | +run with ruby: |
| 17 | + |
| 18 | + $ ruby hello_world_test.rb |
| 19 | + |
| 20 | +This will fail, complaining that there is no file called `hello_world`. |
| 21 | + |
| 22 | +To fix the error create an empty file called `hello_world.rb` in the same |
| 23 | +directory as the `hello_world_test.rb` file. |
| 24 | + |
| 25 | +## Step 2 |
| 26 | + |
| 27 | +Run the test again. It will give you a new error. |
| 28 | + |
| 29 | + 1) Error: |
| 30 | + HelloWorldTest#test_no_name: |
| 31 | + NameError: uninitialized constant HelloWorldTest::HelloWorld |
| 32 | + hello-world/hello_world_test.rb:5:in `test_no_name' |
| 33 | + |
| 34 | +Within the first test, we are referencing a constant named `HelloWorld` when |
| 35 | +we say `HelloWorld.hello`. When Ruby sees a capitalized name like |
| 36 | +`HelloWorld`, it looks it up in a big huge list of all the constants it knows about, |
| 37 | +to see what it points to. It could point to anything, and often in Ruby we have |
| 38 | +constants that point to definitions of classes or modules. |
| 39 | + |
| 40 | +When it looks `HelloWorld` up in it's list, it doesn't find anything, so we need |
| 41 | +to make one. |
| 42 | + |
| 43 | +There are several ways that this error message can be made to go |
| 44 | +away, one of which is to define a `HelloWorld` class. |
| 45 | + |
| 46 | +Open up the hello_world.rb file and add the following code: |
| 47 | + |
| 48 | + class HelloWorld |
| 49 | + end |
| 50 | + |
| 51 | +## Step 3 |
| 52 | + |
| 53 | +Run the test again. |
| 54 | + |
| 55 | + 1) Error: |
| 56 | + HelloWorldTest#test_no_name: |
| 57 | + NoMethodError: undefined method `hello' for HelloWorld:Class |
| 58 | + hello_world_test.rb:5:in `test_no_name' |
| 59 | + |
| 60 | +This time we have a `HelloWorld`, but we're trying tell it to `hello`, and |
| 61 | +`HelloWorld` doesn't understand that message. |
| 62 | + |
| 63 | +Open up hello_world.rb and add a method definition inside the class: |
| 64 | + |
| 65 | + class HelloWorld |
| 66 | + def self.hello |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | +## Step 4 |
| 71 | + |
| 72 | +Run the test again. |
| 73 | + |
| 74 | + 1) Failure: |
| 75 | + HelloWorldTest#test_no_name [hello_world_test.rb:11]: |
| 76 | + When given no name, we should greet the world!. |
| 77 | + Expected: "Hello, world!" |
| 78 | + Actual: nil |
| 79 | + |
| 80 | +Up until now we've been getting errors, this time we get a failure. |
| 81 | + |
| 82 | +An error means that Ruby cannot even run properly because of things like missing |
| 83 | +files or syntax errors, or referring to things that don't exist. |
| 84 | + |
| 85 | +A failure is different. A failure is when Ruby is running just fine |
| 86 | +and the test is expecting one outcome, but getting another. |
| 87 | + |
| 88 | +The test is expecting the `hello` method to return the string `"Hello, world!"`. The easiest way |
| 89 | +to make it pass, is to simply stick the string `"Hello, world!"` inside the method definition. |
| 90 | + |
| 91 | +## Step 6 |
| 92 | + |
| 93 | +Run the test again. |
| 94 | + |
| 95 | +If it fails you're going to need to read the error message carefully to figure |
| 96 | +out what went wrong, and then try again. |
| 97 | + |
| 98 | +If it passes, then you're ready to move to the next step. |
| 99 | + |
| 100 | +Open the hello_world_test.rb file, and find the word "skip". All but the first test |
| 101 | +start with "skip", which tells Minitest to ignore the test. This is so that |
| 102 | +you don't have to deal with all the failures at once. |
| 103 | + |
| 104 | +To activate the next test, delete the "skip", and run the test suite again. |
| 105 | + |
| 106 | +## Wash, Rinse, Repeat |
| 107 | + |
| 108 | +Delete one "skip" at a time, and make each test pass before you move to the |
| 109 | +next one. |
| 110 | + |
| 111 | +## Submit |
| 112 | + |
| 113 | +When everything is passing, you can submit your code with the following |
| 114 | +command: |
| 115 | + |
| 116 | + $ exercism submit hello_world.rb |
| 117 | + |
0 commit comments