RSpec - Tutorialspoint

8m ago
11 Views
2 Downloads
912.67 KB
10 Pages
Last View : 9d ago
Last Download : 3m ago
Upload by : Vicente Bone
Transcription

RSpec About the Tutorial RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the "behavior" of an application being tested. RSpec does not put emphasis on, how the application works but instead on how it behaves, in other words, what the application actually does. This tutorial will show you, how to use RSpec to test your code when building applications with Ruby. Audience This tutorial is for beginners who want to learn how to write better code in Ruby. After finishing this tutorial, you will be able to incorporate RSpec tests into your daily coding practices. Prerequisites In order to benefit from reading this tutorial, you should have some experience with programming, specifically with Ruby. Disclaimer & Copyright Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute, or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness, or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@tutorialspoint.com i

RSpec Table of Contents About the Tutorial . i Audience. i Prerequisites. i Disclaimer & Copyright . i Table of Contents. ii 1. RSPEC – INTRODUCTION . 1 RSpec Environment. 1 2. RSPEC – BASIC SYNTAX . 4 The describe Keyword . 4 The context Keyword . 4 The it Keyword . 5 The expect Keyword . 5 3. RSPEC – WRITING SPECS . 6 4. RSPEC – MATCHERS . 11 Equality/Identity Matchers . 11 Comparison Matchers . 12 Class/Type Matchers . 13 True/False/Nil Matchers . 14 Error Matchers. 15 5. RSPEC – TEST DOUBLES . 17 6. RSPEC – STUBS . 19 7. RSPEC – HOOKS . 22 8. RSPEC – TAGS . 25 ii

RSpec 9. RSPEC – SUBJECTS . 26 10. RSPEC – HELPERS . 28 11. RSPEC – METADATA . 30 12. RSPEC – FILTERING. 33 RSpec Formatters . 34 Failed Examples . 37 13. RSPEC – EXPECTATIONS . 38 iii

1. RSpec – Introduction RSpec RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool. What this means is that, tests written in RSpec focus on the “behavior” of an application being tested. RSpec does not put emphasis on, how the application works but instead on how it behaves, in other words, what the application actually does. RSpec Environment First of all, you will need to install Ruby on your computer. However, if you haven’t already done earlier, then you can download and install Ruby from the main Ruby website: ation. If you are installing Ruby on Windows, you should have the Ruby installer for Windows here at: http://www.rubyinstaller.org For this tutorial, you will only need text editor, such as Notepad and a command line console. The examples here will use cmd.exe on Windows. To run cmd.exe, simply click on the Start menu and type “cmd.exe”, then hit the Return key. At the command prompt in your cmd.exe window, type the following command to see what version of Ruby you are using: ruby -v You should see the below output that looks similar to this: ruby 2.2.3p173 (2015-08-18 revision 51636) [x64-mingw32] The examples in this tutorial will use Ruby 2.2.3 but any version of Ruby higher than 2.0.0 will suffice. Next, we need to install the RSpec gem for your Ruby installation. A gem is a Ruby library which you can use in your own code. In order to install a gem, you need to use the gem command. Let’s install the Rspec gem now. Go back to your cmd.exe Window and type the following: gem install rspec You should have a list of dependent gems that were installed, these are gems that the rspec gem needs to function correctly. At the end of the output, you should see something that looks like this: Done installing documentation for diff-lcs, rspec-support, rspec-mocks, rspecexpectations, rspec-core, rspec after 22 seconds 6 gems installed 1

RSpec Do not worry, if your output does not look exactly the same. 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. Hello World To get started, let’s create a directory (folder) to store our RSpec files. In your cmd.exe window, type the following: cd \ Then type: mkdir rspec tutorial And finally, type: cd rspec tutorial From here, we’re going to create another directory named spec, do that by typing: mkdir spec We are going to store our RSpec files in this folder. RSpec files are known as “specs”. If this seems confusing to you, you can think of a spec file as a test file. RSpec uses the term “spec” which is a short form for “specification”. 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. In behavior driven development, the specification is often described in terms of a “User Story”. RSpec is designed to make it clear whether the target code is behaving correctly, in other words following the specification. Let’s return to our Hello World code. Open a text editor and add the following code: class HelloWorld def say hello "Hello World!" end end describe HelloWorld do context “When testing the HelloWorld class” do it "should say 'Hello World' when we call the say hello method" do hw HelloWorld.new message hw.say hello expect(message).to eq "Hello World!" end end 2

RSpec end Next, save this to a file named hello world spec.rb in the spec folder that you created above. Now back in your cmd.exe window, run this command: rspec spec spec\hello world spec.rb When the command completes, you should see output that looks like this: Finished in 0.002 seconds (files took 0.11101 seconds to load) 1 example, 0 failures Congratulations, you just created and ran your first RSpec unit test! In the next section, we will continue to discuss the syntax of RSpec files. 3

2. RSpec – Basic Syntax RSpec Let’s take a closer look at the code of our HelloWorld example. First of all, in case it isn’t clear, we are testing the functionality of the HelloWorld class. This of course, is a very simple class that contains only one method say hello(). Here is the RSpec code again: describe HelloWorld do context “When testing the HelloWorld class” do it "The say hello method should return 'Hello World'" do hw HelloWorld.new message hw.say hello expect(message).to eq "Hello World!" end end end The describe Keyword The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument. You also need to pass a block argument to describe, this will contain the individual tests, or as they are known in RSpec, the “Examples”. The block is just a Ruby block designated by the Ruby do/end keywords The context Keyword The context keyword is similar to describe. It too can accept a class name and/or string argument. You should use a block with context as well. The idea of context is that it encloses tests of a certain type. For example, you can specify groups of Examples with different contexts like this: context “When passing bad parameters to the foobar() method” context “When passing valid parameters to the foobar() method” context “When testing corner cases with the foobar() method” The context keyword is not mandatory, but it helps to add more details about the examples that it contains. 4

RSpec The it Keyword The word it is another RSpec keyword which is used to define an “Example”. An example is basically a test or a test case. Again, like describe and context, it accepts both class name and string arguments and should be used with a block argument, designated with do/end. In the case of it, it is customary to only pass a string and block argument. The string argument often uses the word “should” and is meant to describe what specific behavior should happen inside the it block. In other words, it describes that expected outcome is for the Example. Note the it block from our HelloWorld Example: it "The say hello method should return 'Hello World'" do The string makes it clear what should happen when we call say hello on an instance of the HelloWorld class. This part of the RSpec philosophy, an Example is not just a test, it’s also a specification (a spec). In other words, an Example both documents and tests the expected behavior of your Ruby code. The expect Keyword The expect keyword is used to define an “Expectation” in RSpec. This is a verification step where we check, that a specific expected condition has been met. From our HelloWorld Example, we have: expect(message).to eql "Hello World!" The idea with expect statements is that they read like normal English. You can say this aloud as “Expect the variable message to equal the string ‘Hello World’”. The idea is that its descriptive and also easy to read, even for non-technical stakeholders such as project managers. The to keyword The to keyword is used as part of expect statements. Note that you can also use the not to keyword to express the opposite, when you want the Expectation to be false. You can see that to is used with a dot, expect(message).to, because it actually just a regular Ruby method. In fact, all of the RSpec keywords are really just Ruby methods. The eql keyword The eql keyword is a special RSpec keyword called a Matcher. You use Matchers to specify what type of condition you are testing to be true (or false). In our HelloWorld expect statement, it is clear that eql means string equality. Note that, there are different types of equality operators in Ruby and consequently different corresponding Matchers in RSpec. We will explore the many different types of Matchers in a later section. 5

RSpec End of ebook preview If you liked what you saw Buy it from our store @ https://store.tutorialspoint.com 6

ruby 2.2.3p173 (2015-08-18 revision 51636) [x64-mingw32] The examples in this tutorial will use Ruby 2.2.3 but any version of Ruby higher than 2.0.0 will suffice. Next, we need to install the RSpec gem for your Ruby installation. A gem is a Ruby library which you can use in your own code. In order to install a gem, you need to use the gem command.

Related Documents:

rspec ./spec/coffee_spec.rb:25 # A cup of coffee with milk costs 1.25 The test report is a list of the specifications of various cups of coffee that RSpec verified. There's a lot of information here, and RSpec uses spacing and capitalization to show you what's going on: An example group lists all of its examples indented underneath it.

RSpec::Mocks.setup to go back into stand-alone mode. Test Doubles Have Short Lifetimes RSpec tears down all your test doubles at the end of each example. That means they won't play well with RSpec features that live outside the typical per-example scope, such as before(:context) hooks. You can work around some of these limitations with a method

This will create a spec folder for your tests, along with the following config files: a spec directory into which to put spec files a spec/spec_helper.rb file with default configuration options an .rspec file with default command-line flags A simple RSpec example In greeter.rb (wherever that goes in your project): class Greeter def greet "Hello, world!"

RSpec is also an excellent tool for processing spectra produced by high-end slit spectrometers. Everything in this document applies to slit spectra also, with the exception of the section on wavelength calibration. If you are using a slit spectrometer, see video 29 at this link for information on using RSpec .

tutorialspoint.com or google.com these are domain names. A domain name has two parts, TLD (Top Level Domain) and SLD (Second level domain), for example in tutorialspoint.com, tutorialspoint is second level domain of TLD .com, or you can say it's a subdomain of .com TLD. There are many top level domains available, like .com,

Prefacetothisedition ThanksforcheckingoutthiseditionofEveryday Rails Testing with RSpec.It’sbeen dyourpatiencehasbeen

Peel.c As long as your telescope can achieve focus with a camera, it will probably work with RSpec and the Star Analyser grating, but my sense is that more focal length is probably better than less for this application. The mount was my Celestron VX GEM, which is light and easy to tote around and has great tr

hubungan antara asupan asam folat dengan kadar Hb dengan nilai p 0,64. Kata Kunci : asupan fe, asupan folat, kadar hb, tb paru . Abstract . Tuberculosis pulmonary can lead to various metabolic disorders and system disturbances in the body, one of which is synthetic disorder of Hemoglobin levels. Some nutrients which can influence the synthetic of Hemoglobin levels are iron (Fe) and folic .