RSpec
   HOME

TheInfoList



OR:

RSpec is a computer domain-specific language (DSL) (particular application domain)
testing tool In software testing, test automation is the use of software separate from the software being tested to control the execution of tests and the comparison of actual outcomes with predicted outcomes. Test automation can automate some repetitive bu ...
written in the
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in production applications. The basic idea behind this concept is that of
test-driven development Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against al ...
(TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by
refactoring In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structu ...
. It contains its ow
mocking framework
that is fully integrated into the framework based upo
JMock
The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications. The RSpec tool can be used by installing the rspec
gem A gemstone (also called a fine gem, jewel, precious stone, or semiprecious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewelry or other adornments. However, certain rocks (such as lapis lazuli, opal, a ...
which consists of three other gems, namely rspec-core, rspec-expectation and rspec-mock.


History

RSpec was started as an experiment by Steven Baker in 2005 along with his team members Dave Astels, Aslak Hellesøy and David Chelimsky. Chelimsky was responsible for developing the RSpec-Rails which facilitated the integration with Ruby on Rails. The initial release i.e. RSpec 1.0 came out in May 2007 which contained many prime features of RSpec which are being included in the latest releases too. However, due to some technical issues such as testing speed, it was discontinued later. The third version of RSpec i.e. the RSpec 3 was released in July 2014 which had many new features like verify doubles, composable matchers and many more.


Usage


Describing the behavior of objects

As mentioned above, RSpec provides a domain-specific language to describe the behavior of
objects Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
. The keywords used in RSpec are similar to the ones used in other languages and/or TDD frameworks. For example, if the keywords used in Test::Unit are considered, they can be mapped to the RSpec keywords as follows: * Assertion becomes ''expectation'' * Test method becomes ''Example code'' * Test case becomes ''Example group'' There are many such keywords which are used in the same context but with the similar names. The syntax of RSpec provides the ease of readability and describes the behavior of the code thereby providing freedom to the programmer. Every testing framework works in the following flow - given some context, when some event occurs, what outcome is expected. The methods like , and form the analogy and the skeleton respectively of the test code.


The method is used to describe a class, method or an example group. This is the outer block which actually contains the test code and it depicts the characteristics of the code enclosed in it. This method takes a number of arguments and an optional block. However, normally one or two arguments are used to describe the behavior of the example group. The first argument represents the reference to the class or module whereas the second argument is optional whose datatype would be String. The example groups can be nested as well. An example of using the describe method is as follows: describe User, "with no account balance" => User with no account balance


The block is used to describe the context in which the class or method mentioned in the describe block is being used. This can be considered as an alias to the word in this scenario and they both can be used interchangeably. Generally, is used for things and is used for contexts. It helps to venture out different outcomes in different scenarios. The example mentioned above can be described using the method as follows: describe User do context "has no account balance" do .... end end Using makes it easier to scan a spec file and makes it clear what it relates to.


It is a RSpec method which describes the specifications of the sample in the context. The block takes a string as an argument and the string after the 'it' keyword can be considered as the function that the block is expected to perform or in other words it can be considered as a test case. Consider the following example: describe User do context "has no account balance" do it "is not allowed to sanction a housing loan" do puts "The loan cannot be sanctioned due to no balance in the account." end end end


RSpec::Expectations

In RSpec, an ''expectation'' is a statement expressing the state that something is expected to be in, at a particular point in the execution of a code example. RSpec uses a simple framework and keywords like ''should()'' and ''should_not()'' to express expectations. It supports matchers, that is objects that try to match an expected outcome, for common operations as well as uncommon expressions. For example, if the expected outcome of a result is say numeric value 5, a RSpec expectation that uses the matcher ''equal'' for the same would be written as follows: result.should equal(5)


RSpec::Mocks

RSpec provides a library called RSpec::Mocks in order to create
test double In computer programming and computer science, programmers employ a technique called automated unit testing to reduce the likelihood of bugs occurring in the software. Frequently, the final release software consists of a complex set of objects or ...
s that facilitate focusing on roles
interface discovery
but most of all making progress without implemented dependencies thereby providing isolation from
coupling A coupling is a device used to connect two shafts together at their ends for the purpose of transmitting power. The primary purpose of couplings is to join two pieces of rotating equipment while permitting some degree of misalignment or end mov ...
and non determinism.


Tools and integration

There are many tools that support RSpec such as: * Autotest
RCov
* Editors such as Vim and Emacs *
IDEs Ides or IDES may refer to: Calendar dates * Ides (calendar), a day in the Roman calendar that fell roughly in the middle of the month. In March, May, July, and October it was the 15th day of the month; in other months it was the 13th. **Ides of Mar ...
such as
Aptana Aptana, Inc. is a company that makes web application development tools for use with a variety of programming languages (such as JavaScript, Ruby, PHP and Python). Aptana's main products include Aptana Studio, Aptana Cloud and Aptana Jaxer. Aptan ...
and
RubyMine JetBrains s.r.o. (formerly IntelliJ Software s.r.o.) is a Czech software development company which makes tools for software developers and project managers. , the company has offices in Prague; Munich; Berlin; Boston, Massachusetts; Amsterda ...
.
RakeTask
*
TextMate TextMate is a general-purpose GUI text editor for macOS created by Allan Odgaard. TextMate features declarative customizations, tabs for open documents, recordable macros, folding sections, snippets, shell integration, and an extensible bund ...
RSpec also provides a number of utilities and extension points to support extending RSpec to meet domain-specific needs. For instance, using Metadata associated with groups and examples for the purpose of reporting, usin
global configuration
to assign before and after blocks to every example group, using macros etc.


Other Ruby testing tools

* Test::Unit * MiniTest * Cucumber * Shoulda * Capybara etc.


References


Further reading

1. Getting started with RSpec: (installation steps) http://rspec.info/ 2. Beck, K. (2014)
''Test-driven development by example''.
Boston: Addison-Wesley.


External links


Official website

RSpec-2

Beyond Test Driven Development - Behaviour Driven Development.
A Google TechTalk by Dave Astels on BDD using RSpec.
RSpec Best Practices
Article by Jared Carroll, retrieved April 24, 2011.
Behavior-driven testing with RSpec
Article by Bruce Tate, retrieved July 21, 2011. {{Ruby programming language Free software testing tools Software using the MIT license Free software programmed in Ruby