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")
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
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
} 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
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
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
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
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
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
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
3 end 4 5 describe MyClass do 6 before(:example) {|example| puts example.metadata } 7 let(:example_description) {|example| example.description } 8 9 it "accesses the example" do |example| 10 ¦ puts example.metadata 11 end 12 end Example Semantics
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
end_with("z") 4 5 # Now, can be... 6 expect(alphabet).to start_with("a").and end_with("z") 7 8 # You can also use "or"... 9 expect(stoplight.color).to eq("red").or eq("green")
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)
• 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