Create a gist now

Instantly share code, notes, and snippets.

Example of what is possible with RSpec2

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 ........
# 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
@igrigorik

Nice :-)

@sgerrand

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

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

@bingxie
bingxie commented Nov 22, 2013

great, we can use subject directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment