Jump to content

RSpec

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Kiwi128 (talk | contribs) at 22:59, 31 August 2018 (move tag to more appropriate location). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

RSpec
Developer(s)David Chelimsky, Myron Marston, Andy Lindeman, Jon Rowe, Paul Casaretto, Sam Phippen, Bradley Schaefer[1]
Initial releaseMay 18, 2007; 17 years ago (2007-05-18)[2]
Stable release
3.8.0 / August 4, 2018 (2018-08-04)[3]
Repository
Operating systemCross-platform
TypeBehavior driven development framework / Test tool
LicenseMIT License
Websiterspec.info

RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby to test Ruby code.[4] It is a behavior-driven development (BDD) framework which is extensively used in the production applications. The basic idea behind this concept is that of Test Driven Development (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. It contains its own mocking framework that is fully integrated into the framework based upon JMock.[5] 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 which consists of 3 other gems namely rspec-core, rspec-expectation and rspec-mock.[4]

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. David 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 also. 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. The latest version of the RSpec currently available is RSpec 3.7.

Usage

Describing the behavior of objects

As mentioned above, RSpec provides a domain-specific language to describe the behavior of objects. The keywords used in RSpec are similar to the ones used in other languages and/or TDD frameworks.[6] 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 describe(), context() and it() form the analogy and the skeleton respectively of the test code.

describe()

The describe() 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.[6] 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.[6] The example groups can be nested as well. An example of using the describe method is as follows:[6]

describe User, "with no account balance" {....}
=> User with no account balance

[6]

context()

The context() 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 describe() in this scenario and they both can be used interchangeably. Generally, describe() is used for things and context() is used for contexts. It helps to venture out different outcomes in different scenarios. The example mentioned above can be described using the context() method as follows:[6]

describe User do
    context "has no account balance" do
        ....
    end
end

[6] Using context() makes it easier to scan a spec file and makes it clear what it relates to.

it()

It is a RSpec method which describes the specifications of the sample in the context. The it() 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:[6]

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

[6]

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:[6]

result.should equal(5)

RSpec::Mocks

RSpec provides a library called RSpec::Mocks in order to create test doubles that facilitate focusing on roles, interface discovery, but most of all making progress without implemented dependencies thereby providing isolation from coupling and non determinism.[6]

Tools and Integration

There are many tools that support RSpec such as:

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, using global configuration to assign before and after blocks to every example group, using macros etc.

Other Ruby testing tools

See also

References

  1. ^ RSpec core team. Retrieved 8 April 2013.
  2. ^ "all versions of rspec". rubygems.org. Retrieved 11 February 2014.
  3. ^ "Releases - rspec/rspec". Retrieved 31 August 2018 – via GitHub.
  4. ^ a b "Introduction to RSpec | The Odin Project". www.theodinproject.com. Retrieved 2017-02-18.
  5. ^ "jMock - An Expressive Mock Object Library for Java". www.jmock.org. Retrieved 2017-02-18.
  6. ^ a b c d e f g h i j k "The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends by David Chelimsky, Dave Astels, Zach Dennis, Aslak Hellesøy, Bryan Helmkamp, Dan North | The Pragmatic Bookshelf". pragprog.com. Retrieved 2017-02-11.

Further reading

1. Getting started with RSpec: (installation steps) http://rspec.info/

2. Beck, K. (2014). Test-driven development by example. Boston: Addison-Wesley.