3/1/2024 0 Comments Rspec tutorialIt "The say_hello method should return 'Hello World'" do This of course, is a very simple class that contains only one method say_hello(). First of all, in case it isn’t clear, we are testing the functionality of the HelloWorld class. Let’s take a closer look at the code of our HelloWorld example. In the next section, we will continue to discuss the syntax of RSpec files. When the command completes, you should see output that looks like this −įinished in 0.002 seconds (files took 0.11101 seconds to load)Ĭongratulations, you just created and ran your first RSpec unit test! Now back in your cmd.exe window, run this command − Next, save this to a file named hello_world_spec.rb in the spec folder that you created above. It "should say 'Hello World' when we call the say_hello method" do Open a text editor and add the following code −Ĭontext “When testing the HelloWorld class” do RSpec is designed to make it clear whether the target code is behaving correctly, in other words following the specification. In behavior driven development, the specification is often described in terms of a “User Story”. Since, RSpec is a BDD test tool, the goal is to focus on what the application does and whether or not it follows a specification. RSpec uses the term “spec” which is a short form for “specification”. If this seems confusing to you, you can think of a spec file as a test file. We are going to store our RSpec files in this folder. In your cmd.exe window, type the following −įrom here, we’re going to create another directory named spec, do that by typing − To get started, let’s create a directory (folder) to store our RSpec files. Also, if you are using a Mac or Linux computer, you may need to either run gem install rspec command using sudo or use a tool like HomeBrew or RVM to install the rspec gem. Rspec-expectations, rspec-core, rspec after 22 secondsĭo not worry, if your output does not look exactly the same. At the end of the output, you should see something that looks like this −ĭone installing documentation for diff-lcs, rspec-support, rspec-mocks, You should have a list of dependent gems that were installed, these are gems that the rspec gem needs to function correctly. Go back to your cmd.exe Window and type the following − In order to install a gem, you need to use the gem command. A gem is a Ruby library which you can use in your own code. Next, we need to install the RSpec gem for your Ruby installation. The examples in this tutorial will use Ruby 2.2.3 but any version of Ruby higher than 2.0.0 will suffice. You should see the below output that looks similar to this − To run cmd.exe, simply click on the Start menu and type “cmd.exe”, then hit the Return key.Īt the command prompt in your cmd.exe window, type the following command to see what version of Ruby you are using − The examples here will use cmd.exe on Windows. If you are installing Ruby on Windows, you should have the Ruby installer for Windows here at − įor this tutorial, you will only need text editor, such as Notepad and a command line console. However, if you haven’t already done earlier, then you can download and install Ruby from the main Ruby website − Ruby. RSpec Environmentįirst of all, you will need to install Ruby on your computer. RSpec does not put emphasis on, how the application works but instead on how it behaves, in other words, what the application actually does. What this means is that, tests written in RSpec focus on the “behavior” of an application being tested. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. RSpec is a unit test framework for the Ruby programming language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |