public
Last active

Example of what is possible with RSpec2

  • Download Gist
gistfile1.md
Markdown

Install RSpec

$ gem install rspec

Run it!

$ rspec --color --format=d register.rb
CashRegister
  when empty
    items
      should be empty
    total
      should be == 0
  after scanning 1 and 2
    items
      should not be empty
    total
      should be == 3

Finished in 0.00697 seconds
4 examples, 0 failures
  • --color - adds green/yellow/red colors to the output.
  • --format=d - prints readable text instead of a bunch of ........
register.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# From: http://rubylearning.com/blog/2011/07/28/how-do-i-test-my-code-with-minitest/
class CashRegister
attr_reader :items
 
def initialize
@items = []
end
 
def scan(item)
@items << item
end
 
def total
@items.inject(0) {|sum, item| sum += item }
end
end
 
require 'rspec'
 
describe CashRegister do
context "when empty" do
its(:items) { should be_empty }
its(:total) { should be == 0 }
end
 
context "after scanning 1 and 2" do
before do
subject.scan(1)
subject.scan(2)
end
 
its(:items) { should_not be_empty }
its(:total) { should be == (1 + 2) }
end
end

--format=d - prints readable text instead of a bunch of ........

Belated: --format=d translates to --format=documentation, for those interested.

great, we can use subject directly.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.