Main Content

Improved test in Cucumber with RSpec

Archive - Originally posted on "The Horse's Mouth" - 2015-01-03 12:39:24 - Graham Ellis

 
Articles in this "Introduction to Cucumber" series:
[link] - Installing Cucumber for Ruby
[link] - Hello Test Cucumber World
[link] - First real tests / second example
[link] - Improved tests with RSpec
 




pass and fail tests are very basic - so we can use other Ruby gems (such as Rspec) to provide shorter and more flexible checking within our tests. Beware - installing rspec on my system replaced rather than supplemented the previous tests.

  WomanWithCat:cuc grahamellis$ sudo gem install rspec
  Password:
  Fetching: rspec-support-3.1.2.gem (100%)
  Successfully installed rspec-support-3.1.2
  Fetching: rspec-core-3.1.7.gem (100%)
  Successfully installed rspec-core-3.1.7
  Fetching: rspec-expectations-3.1.2.gem (100%)
  Successfully installed rspec-expectations-3.1.2
  Fetching: rspec-mocks-3.1.3.gem (100%)
  Successfully installed rspec-mocks-3.1.3
  Fetching: rspec-3.1.0.gem (100%)
  Successfully installed rspec-3.1.0
  Parsing documentation for rspec-support-3.1.2
  Installing ri documentation for rspec-support-3.1.2
  Parsing documentation for rspec-core-3.1.7
  Installing ri documentation for rspec-core-3.1.7
  Parsing documentation for rspec-expectations-3.1.2
  Installing ri documentation for rspec-expectations-3.1.2
  Parsing documentation for rspec-mocks-3.1.3
  Installing ri documentation for rspec-mocks-3.1.3
  Parsing documentation for rspec-3.1.0
  Installing ri documentation for rspec-3.1.0
  5 gems installed
  WomanWithCat:cuc grahamellis$


To use Rspec, my step implementation now includes

  require 'rspec/expectations'

and I have replaced the "if xxxx pass else fail end" structure with

  expect(@capacity).to equal(arg1.to_i)

You may find on older installations you'll use

  @capacity.should == arg1.to_i

instead, but that has been deprecated and is inefficient as it requires a should method on every object!

See complete file [here].