Main Content

RSpec - Ruby testing (stand alone example / no cucumber)

Archive - Originally posted on "The Horse's Mouth" - 2015-10-17 12:46:05 - Graham Ellis

RSpec can be used as a stand alone test environment for your Ruby program, as well as being used within Gherkin and Cucumber (see [here] for some details of that).

Installation as a Ruby Gem (you may need admin permissions):
  gem install rspec

Run your program under the new executable rspec ... on Linux / Unix / OS X start your file with
  #!/usr/bin/env rspec

Load the code to be tested - perhaps this class
  class StringChanger
    def reverse_and_save(string_to_reverse)
      string_to_reverse.reverse
    end
  end


and describe the test

  describe StringChanger do
    it 'reverses strings' do
      string_changer = StringChanger.new
      reversed_string = string_changer.reverse_and_save('example from Well House')
      expect(reversed_string).to eq 'esuoH lleW morf elpmaxe'
      end
  end


run it

  WomanWithCat:rspec grahamellis$ ./rs001
  .
  
  Finished in 0.00073 seconds (files took 0.09143 seconds to load)
  1 example, 0 failures
  
  WomanWithCat:rspec grahamellis$


and see that the tests have passed.

Next lessons - multiple tests, what happens when a test fails, other tests ...