![]() Here RSpec is telling us that it can't find a descending_order method in the file we're testing, which makes sense because we haven't written one yet. This line here is the actual error that occurred. The next line tells us where in our test the failure occurred, in this case it failed when we assigned the descending_order method to the ordered_1 variable. It's telling us what the method is supposed to be doing but isn't.įailure/Error: ordered_1 = descending_order(1587956342) This first line is our it block from our test. Lets break it down line by line.ġ) descending_order takes in any integer and returns its digits in descending order Running your tests should give you output similar to this: Appending -fail-fast or simply -f-f will show you one failing test at a time. Pro-tip: As you start working with larger test suites, it can become overwhelming to run your tests and see dozens of failing examples. Alternately, and this is useful if you have a very large test suite, you can run specific test files like so: rspec spec/descending_order_spec.rb. Run the test by typing rspec into your terminal. If your filenames don't follow that convention, RSpec won't be able to find them.Īt the top of spec/descending_order_spec.rb require your spec_helper file and your descending_order.rb file.Įnter fullscreen mode Exit fullscreen mode When you run RSpec, it will look in your spec folder for any _spec.rb files, and it'll run those. Whenever you create a file to write tests in, it must be located in the spec folder, and it must follow this convention: _spec.rb. Now in your spec directory, create a descending_order_spec.rb file. Lets start by creating a descending_order.rb file where we'll code our solution. Essentially, rearrange the digits to create the highest possible number. Your task is to make a function that can take any non-negative integer as a argument and return it with its digits in descending order. The Codewars challenge we'll be working with today is called Descending Order. These files are for configuration and you don't need to worry about them right now, except to say that we will need to require our spec_helper.rb file in the file we write our tests in. rspec file in your project directory, and a spec folder with a spec_helper.rb file inside it. You will see that this command created a. It is, however, the way you would do it for a larger project, so I'm teaching it this way on purpose so you understand how to properly install, set up, and use RSpec.įrom your project directory, run gem install rspec. Keep in mind as we do this that the file structure I'm using is overkill for the task of solving a simple code challenge. We'll start from scratch and learn how to write a test while solving a basic Codewars challenge using Test Driven Development (TDD). So let's learn RSpec from the ground up and give this testing thing a try. With a little practice, writing tests is actually fun. ![]() Testing can be intimidating, especially for beginners, but it really doesn't have to be that way.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |