# Truthiness and existentialism expect(actual).to be_truthy # passes if actual is truthy (not nil or false) expect(actual).to be true # passes if actual == true expect(actual).to be_falsey # passes if actual is falsy (nil or false) expect(actual).to be false # passes if actual == false expect(actual).to be_nil # passes if actual is nil # Expecting errors expect { do_something }.to raise_error expect { do_something }.to raise_error(ErrorClass) expect { do_something }.to raise_error("message") expect { do_something }.to raise_error(ErrorClass, "message")
1 class Calculator 2 def add(first, second) 3 ¦ first + second 4 end 5 6 def multiply(first, second) 7 ¦ first * second 8 end 9 10 def divide(first, second) 11 ¦ first / second 12 end 13 end
1 describe "Calculator" do 2 it "should #add numbers 2 and 3 to get 5" do 3 ¦ calculator = Calculator.new 4 ¦ expect(calculator.add(2, 3)).to eql(5) 5 end 6 7 it "should #add numbers -2 and 3 to get 1" do 8 ¦ calculator = Calculator.new 9 ¦ expect(calculator.add(-2, 3)).to eql(1) 10 end 11 12 it "should #multiply 2 and 3 to get 6" do 13 ¦ calculator = Calculator.new 14 ¦ expect(calculator.multiply(2, 3)).to eql(6) 15 end 16 17 it "should #multiply -2 and 3 to get -6" do 18 ¦ calculator = Calculator.new 19 ¦ expect(calculator.multiply(-2, 3)).to eql(-6) 20 end 21 22 it "should #divide 6 and 2 to get 3" do 23 ¦ calculator = Calculator.new 24 ¦ expect(calculator.divide(6, 2)).to eql(3) 25 end 26 27 it "should #divide 3 and 0 to raise ZeroDivisionError" do 28 ¦ calculator = Calculator.new 29 ¦ expect { calculator.divide(3, 0) }.to raise_error(ZeroDivisionError) 30 end 31 end
[[email protected]:scratch/calculator]$ rspec spec ! Calculator should #multiply 2 and 3 to get 6 should #divide 6 and 2 to get 3 should #add numbers -2 and 3 to get 1 should #divide 3 and 0 to raise ZeroDivisionError should #add numbers 2 and 3 to get 5 ! Finished in 0.00176 seconds (files took 0.09221 seconds to load) 5 examples, 0 failures
1 describe "Calculator" do 2 before { @calculator = Calculator.new } 3 4 it "should #add numbers 2 and 3 to get 5" do 5 ¦ expect(@calculator.add(2, 3)).to eql(5) 6 end 7 8 it "should #add numbers -2 and 3 to get 1" do 9 ¦ expect(@calculator.add(-2, 3)).to eql(1) 10 end 11 12 it "should #multiply 2 and 3 to get 6" do 13 ¦ expect(@calculator.multiply(2, 3)).to eql(6) 14 end 15 16 it "should #multiply -2 and 3 to get -6" do 17 ¦ expect(@calculator.multiply(-2, 3)).to eql(-6) 18 end 19 20 it "should #divide 6 and 2 to get 3" do 21 ¦ expect(@calculator.divide(6, 2)).to eql(3) 22 end 23 24 it "should #divide 3 and 0 to raise ZeroDivisionError" do 25 ¦ expect { @calculator.divide(3, 0) }.to raise_error(ZeroDivisionError) 26 end 27 end
1 describe "Calculator" do 2 let(:calculator) { Calculator.new } 3 4 it "should #add numbers 2 and 3 to get 5" do 5 ¦ expect(calculator.add(2, 3)).to eql(5) 6 end 7 8 it "should #add numbers -2 and 3 to get 1" do 9 ¦ expect(calculator.add(-2, 3)).to eql(1) 10 end 11 12 it "should #multiply 2 and 3 to get 6" do 13 ¦ expect(calculator.multiply(2, 3)).to eql(6) 14 end 15 16 it "should #multiply -2 and 3 to get -6" do 17 ¦ expect(calculator.multiply(-2, 3)).to eql(-6) 18 end 19 20 it "should #divide 6 and 2 to get 3" do 21 ¦ expect(calculator.divide(6, 2)).to eql(3) 22 end 23 24 it "should #divide 3 and 0 to raise ZeroDivisionError" do 25 ¦ expect { calculator.divide(3, 0) }.to raise_error(ZeroDivisionError) 26 end 27 end
1 describe "Calculator" do 2 let!(:calculator) { Calculator.new } 3 4 it "should #add numbers 2 and 3 to get 5" do 5 ¦ expect(calculator.add(2, 3)).to eql(5) 6 end 7 8 it "should #add numbers -2 and 3 to get 1" do 9 ¦ expect(calculator.add(-2, 3)).to eql(1) 10 end 11 12 it "should #multiply 2 and 3 to get 6" do 13 ¦ expect(calculator.multiply(2, 3)).to eql(6) 14 end 15 16 it "should #multiply -2 and 3 to get -6" do 17 ¦ expect(calculator.multiply(-2, 3)).to eql(-6) 18 end 19 20 it "should #divide 6 and 2 to get 3" do 21 ¦ expect(calculator.divide(6, 2)).to eql(3) 22 end 23 24 it "should #divide 3 and 0 to raise ZeroDivisionError" do 25 ¦ expect { calculator.divide(3, 0) }.to raise_error(ZeroDivisionError) 26 end 27 end
1 describe "Calculator" do 2 subject { Calculator.new } 3 4 it "should #add numbers 2 and 3 to get 5" do 5 ¦ expect(subject.add(2, 3)).to eql(5) 6 end 7 8 it "should #add numbers -2 and 3 to get 1" do 9 ¦ expect(subject.add(-2, 3)).to eql(1) 10 end 11 12 it "should #multiply 2 and 3 to get 6" do 13 ¦ expect(subject.multiply(2, 3)).to eql(6) 14 end 15 16 it "should #multiply -2 and 3 to get -6" do 17 ¦ expect(subject.multiply(-2, 3)).to eql(-6) 18 end 19 20 it "should #divide 6 and 2 to get 3" do 21 ¦ expect(subject.divide(6, 2)).to eql(3) 22 end 23 24 it "should #divide 3 and 0 to raise ZeroDivisionError" do 25 ¦ expect { subject.divide(3, 0) }.to raise_error(ZeroDivisionError) 26 end 27 end
1 describe Calculator do 2 it "should #add numbers 2 and 3 to get 5" do 3 ¦ expect(subject.add(2, 3)).to eql(5) 4 end 5 6 it "should #add numbers -2 and 3 to get 1" do 7 ¦ expect(subject.add(-2, 3)).to eql(1) 8 end 9 10 it "should #multiply 2 and 3 to get 6" do 11 ¦ expect(subject.multiply(2, 3)).to eql(6) 12 end 13 14 it "should #multiply -2 and 3 to get -6" do 15 ¦ expect(subject.multiply(-2, 3)).to eql(-6) 16 end 17 18 it "should #divide 6 and 2 to get 3" do 19 ¦ expect(subject.divide(6, 2)).to eql(3) 20 end 21 22 it "should #divide 3 and 0 to raise ZeroDivisionError" do 23 ¦ expect { subject.divide(3, 0) }.to raise_error(ZeroDivisionError) 24 end 25 end
1 describe Calculator do 2 describe "addition" do 3 ¦ it "should #add numbers 2 and 3 to get 5" do 4 ¦ ¦ expect(subject.add(2, 3)).to eql(5) 5 ¦ end 6 7 ¦ it "should #add numbers -2 and 3 to get 1" do 8 ¦ ¦ expect(subject.add(-2, 3)).to eql(1) 9 ¦ end 10 end 11 12 describe "multiplication" do 13 ¦ it "should #multiply 2 and 3 to get 6" do 14 ¦ ¦ expect(subject.multiply(2, 3)).to eql(6) 15 ¦ end 16 17 ¦ it "should #multiply -2 and 3 to get -6" do 18 ¦ ¦ expect(subject.multiply(-2, 3)).to eql(-6) 19 ¦ end 20 end 21 22 describe "division" do 23 ¦ it "should #divide 6 and 2 to get 3" do 24 ¦ ¦ expect(subject.divide(6, 2)).to eql(3) 25 ¦ end 26 27 ¦ it "should #divide 3 and 0 to raise ZeroDivisionError" do 28 ¦ ¦ expect { subject.divide(3, 0) }.to raise_error(ZeroDivisionError) 29 ¦ end 30 end 31 end
[[email protected]:scratch/calculator]$ rspec spec ! Calculator division should #divide 6 and 2 to get 3 should #divide 3 and 0 to raise ZeroDivisionError addition should #add numbers 2 and 3 to get 5 should #add numbers -2 and 3 to get 1 multiplication should #multiply 2 and 3 to get 6 should #multiply -2 and 3 to get -6 ! Finished in 0.00211 seconds (files took 0.09233 seconds to load) 6 examples, 0 failures
Pending Semantics 1 describe Penguin do 2 # This will fail! When using pending, test must fail now. 3 it "should do something" do 4 ¦ pending 5 ¦ expect(true).to eq(true) 6 end 7 8 skip "not implemented yet" do 9 end 10 11 it "does something", :skip => true do 12 end 13 14 it "does something", :skip => "reason explanation" do 15 end 16 17 it "does something else" do 18 ¦ skip "reason explanation" 19 end 20 end
New “between” Matcher 1 # like `Comparable#between?`, it is inclusive by default 2 expect(10).to be_between(5, 10) 3 4 # ...but you can make it exclusive: 5 expect(10).not_to be_between(5, 10).exclusive 6 7 # ...or explicitly label it inclusive: 8 expect(10).to be_between(5, 10).inclusive
rspec-mocks Syntax 1 # old syntax: 2 object.stub(foo: 1, bar: 2) 3 4 # new syntax: 5 allow(object).to receive_messages(foo: 1, bar: 2) 6 7 # also works with expect: 8 expect(object).to receive_messages(foo: 1, bar: 2)
Other Changes • Removed support for Ruby 1.8.6 / 1.9.1 • Better Ruby 2 support (keyword arguments, prepended modules) • Internal refactoring & housekeeping: breaking out to other gems • Completely new RSpec formatter (rspec-legacy_formatters still available) • Can disable exposing DSL globally (need to use RSpec.describe): config.expose_dsl_globally = false • Example group aliases: config.alias_example_group_to :describe_model, :type => :model • Skipped group aliases: xdescribe, xcontext, xit • Focused group aliases: fdescribe, fcontext, fit