This tutorial will show you, how to use RSpec to test your code when building applications with Ruby.

Similar documents
This tutorial will show you, how to use RSpec to test your code when building applications with Ruby.

This is a brief tutorial that explains how to make use of Sqoop in Hadoop ecosystem.

This is an introductory tutorial designed for beginners to help them understand the basics of Radius.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Before you start with this tutorial, you need to know basic Java programming.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Meteor

This tutorial has been prepared for beginners to help them understand the simple but effective SEO characteristics.

This tutorial provides a basic understanding of how to generate professional reports using Pentaho Report Designer.

This tutorial is designed for software programmers who would like to learn the basics of ASP.NET Core from scratch.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

This tutorial also elaborates on other related methodologies like Agile, RAD and Prototyping.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. TurboGears

Parrot is a virtual machine designed to efficiently compile and execute bytecode for interpreted languages.

This tutorial has been prepared for beginners to help them understand the basic functionalities of Gulp.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Django

This tutorial provides a basic level understanding of the LOLCODE programming language.

SAP Lumira is known as a visual intelligence tool that is used to visualize data and create stories to provide graphical details of the data.

Scalable Vector Graphics commonly known as SVG is a XML based format to draw vector images. It is used to draw twodimentional vector images.

In this tutorial, we are going to learn how to use the various features available in Flexbox.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Jenkins

Testing is the process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not.

Microsoft Excel is a spreadsheet tool capable of performing calculations, analyzing data and integrating information from different programs.

You must have a basic understanding of GNU/Linux operating system and shell scripting.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. RichFaces

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer. PySpark

This tutorial explains the key concepts of Web Dynpro with relevant screenshots for better understanding.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright DAX

This tutorial provides a basic understanding of the infrastructure and fundamental concepts of managing an infrastructure using Chef.

This tutorial will guide users on how to utilize TestLodge in reporting and maintaining the testing activities.

Memcached is an open source, high-performance, distributed memory object caching system.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer ASP.NET WP

So, this tutorial is divided into various chapters and describes the 5G technology, its applications, challenges, etc., in detail.

jmeter is an open source testing software. It is 100% pure Java application for load and performance testing.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Graph Theory

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer. Logstash

This tutorial explains how you can use Gradle as a build automation tool for Java as well as Groovy projects.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Laravel

This tutorial will take you through simple and practical approaches while learning AOP framework provided by Spring.

This tutorial has been designed for beginners interested in learning the basic concepts of UDDI.

This tutorial will give you a quick start with Consul and make you comfortable with its various components.

This tutorial covers a foundational understanding of IPC. Each of the chapters contain related topics with simple and useful examples.

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Gerrit

This tutorial is prepared for beginners to help them understand the basic-to-advanced concepts related to GPRS.

This tutorial will help you in understanding IPv4 and its associated terminologies along with appropriate references and examples.

Before proceeding with this tutorial, you should have a good understanding of the fundamental concepts of marketing.

This tutorial is designed for all Java enthusiasts who want to learn document type detection and content extraction using Apache Tika.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. WordPress

This is an introductory tutorial, which covers the basics of Jython and explains how to handle its various modules and sub-modules.

This tutorial helps the professionals aspiring to make a career in Big Data and NoSQL databases, especially the documents store.

You should have a basic understanding of Relational concepts and basic SQL. It will be good if you have worked with any other RDBMS product.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer DBMS

This tutorial is designed for those who would like to understand the basics of i-mode in simple and easy steps.

This is a small tutorial where we will cover all the basic steps needed to start with Balsamiq Mockups.

In mainframe environment, programs can be executed in batch and online modes. JCL is used for submitting a program for execution in batch mode.

This tutorial will help computer science graduates to understand the basic-to-advanced concepts related to data warehousing.

About Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Euphoria

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Apache Bench

This is a simple tutorial that covers the basics of SAP Business Intelligence and how to handle its various other components.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Joomla

This tutorial is designed for all those software professionals who are keen on learning the basics of Clojure and how to put it into practice.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Drupal

This tutorial discusses the basics of PouchDB along with relevant examples for easy understanding.

This tutorial introduces you to key DynamoDB concepts necessary for creating and deploying a highly-scalable and performance-focused database.

Dart is an open-source general-purpose programming language. It is originally developed by Google and later approved as a standard by ECMA.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

In this brief tutorial, we will be explaining the basics of Elasticsearch and its features.

Before you start proceeding with this tutorial, we are assuming that you are already aware about the basics of Web development.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

This tutorial is designed for software developers who want to learn how to develop quality applications with clean structure of code.

Lecture 3. Miscellaneous Ruby and Testing

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. HCatalog

This tutorial has been prepared for computer science graduates to help them understand the basic-to-advanced concepts related to data mining.

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Adobe Flex Tutorial i

Lecture 3. Miscellaneous Ruby and Testing 1 / 48

This tutorial is meant for software developers who want to learn how to lose less time on API integrations!

This tutorial covers various topics ranging from the basics of Lua to its scope in various applications.

In this tutorial, we will discuss the architecture, pin diagram and other key concepts of microprocessors.

This tutorial has been designed to help beginners understand the basic concepts of WiMAX.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Avro

Before proceeding with this tutorial, you must have a good understanding of Core Java and any of the Linux flavors.

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright CICS

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Effective Testing with RSpec 3

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

This tutorial is going to help all those readers who want to learn the basics of WSDL and use its features to interface with XML-based services.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Computer Graphics

Lecture 3. Miscellaneous Ruby and Testing 1 / 40

Learn C programme. C Programming language. Cloud1 C tutorial

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Try the following example using the Try it option available at the top right corner of the below sample code box

Programming for Kids

One of the hardest things you have to do is to keep track of three kinds of commands when writing and running computer programs. Those commands are:

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Science One CS : Getting Started

Using GitHub to Share with SparkFun a

Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

Cognos also provides you an option to export the report in XML or PDF format or you can view the reports in XML format.

Transcription:

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

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

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: https://www.ruby-lang.org/en/documentation/installation. 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 depent gems that were installed, these are gems that the rspec gem needs to function correctly. At the 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

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!" 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!" 2

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!" 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/ 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

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/. 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

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