spec/rspec/matchers/matcher_spec.rb in rspec-expectations-2.12.1 vs spec/rspec/matchers/matcher_spec.rb in rspec-expectations-2.13.0

- old
+ new

@@ -31,12 +31,12 @@ end m1 = example_matcher(1) m2 = example_matcher(2) - m1.matches?(1).should be_true - m2.matches?(2).should be_true + expect(m1.matches?(1)).to be_true + expect(m2.matches?(2)).to be_true end context "with an included module" do let(:matcher) do RSpec::Matchers::DSL::Matcher.new(:be_a_greeting) do @@ -48,23 +48,23 @@ it "has access to the module's methods" do matcher.matches?("Hello, World") end it "runs the module's included hook" do - matcher.should respond_to(:included_method) + expect(matcher).to respond_to(:included_method) end it "does not run the module's extended hook" do - matcher.should_not respond_to(:extended_method) + expect(matcher).not_to respond_to(:extended_method) end it 'allows multiple modules to be included at once' do m = RSpec::Matchers::DSL::Matcher.new(:multiple_modules) do include Enumerable, Comparable end.for_expected - m.should be_a(Enumerable) - m.should be_a(Comparable) + expect(m).to be_a(Enumerable) + expect(m).to be_a(Comparable) end end context "without overrides" do before(:each) do @@ -74,21 +74,21 @@ end end.for_expected(3) end it "provides a default description" do - @matcher.description.should == "be a multiple of 3" + expect(@matcher.description).to eq "be a multiple of 3" end it "provides a default failure message for #should" do @matcher.matches?(8) - @matcher.failure_message_for_should.should == "expected 8 to be a multiple of 3" + expect(@matcher.failure_message_for_should).to eq "expected 8 to be a multiple of 3" end it "provides a default failure message for #should_not" do @matcher.matches?(9) - @matcher.failure_message_for_should_not.should == "expected 9 not to be a multiple of 3" + expect(@matcher.failure_message_for_should_not).to eq "expected 9 not to be a multiple of 3" end end context "with separate match logic for should and should not" do let(:matcher) do @@ -102,75 +102,75 @@ end end.for_expected(7, 11) end it "invokes the match_for_should block for #matches?" do - matcher.matches?(77).should be_true - matcher.matches?(18).should be_false + expect(matcher.matches?(77)).to be_true + expect(matcher.matches?(18)).to be_false end it "invokes the match_for_should_not block for #does_not_match?" do - matcher.does_not_match?(77).should be_false - matcher.does_not_match?(18).should be_true + expect(matcher.does_not_match?(77)).to be_false + expect(matcher.does_not_match?(18)).to be_true end it "provides a default failure message for #should_not" do matcher.does_not_match?(77) - matcher.failure_message_for_should_not.should == "expected 77 not to to be composed of 7 and 11" + expect(matcher.failure_message_for_should_not).to eq "expected 77 not to to be composed of 7 and 11" end end it "allows helper methods to be defined with #define_method to have access to matcher parameters" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |a, b| define_method(:sum) { a + b } end.for_expected(3,4) - matcher.sum.should == 7 + expect(matcher.sum).to eq 7 end it "is not diffable by default" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) {} - matcher.should_not be_diffable + expect(matcher).not_to be_diffable end it "is diffable when told to be" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) { diffable }.for_expected - matcher.should be_diffable + expect(matcher).to be_diffable end it "provides expected" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) {}.for_expected('expected string') - matcher.expected.should == ['expected string'] + expect(matcher.expected).to eq ['expected string'] end it "provides actual" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do match {|actual|} end.for_expected('expected string') matcher.matches?('actual string') - matcher.actual.should == 'actual string' + expect(matcher.actual).to eq 'actual string' end context "wrapping another expectation (should == ...)" do it "returns true if the wrapped expectation passes" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |expected| match do |actual| - actual.should == expected + expect(actual).to eq expected end end.for_expected('value') - matcher.matches?('value').should be_true + expect(matcher.matches?('value')).to be_true end it "returns false if the wrapped expectation fails" do matcher = RSpec::Matchers::DSL::Matcher.new(:name) do |expected| match do |actual| - actual.should == expected + expect(actual).to eq expected end end.for_expected('value') - matcher.matches?('other value').should be_false + expect(matcher.matches?('other value')).to be_false end end context "with overrides" do before(:each) do @@ -189,49 +189,49 @@ end end.for_expected(true) end it "does not hide result of match block when true" do - @matcher.matches?(true).should be_true + expect(@matcher.matches?(true)).to be_true end it "does not hide result of match block when false" do - @matcher.matches?(false).should be_false + expect(@matcher.matches?(false)).to be_false end it "overrides the description" do - @matcher.description.should == "be the boolean true" + expect(@matcher.description).to eq "be the boolean true" end it "overrides the failure message for #should" do @matcher.matches?(false) - @matcher.failure_message_for_should.should == "expected false to be the boolean true" + expect(@matcher.failure_message_for_should).to eq "expected false to be the boolean true" end it "overrides the failure message for #should_not" do @matcher.matches?(true) - @matcher.failure_message_for_should_not.should == "expected true not to be the boolean true" + expect(@matcher.failure_message_for_should_not).to eq "expected true not to be the boolean true" end end context "#new" do it "passes matches? arg to match block" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do match do |actual| actual == 5 end end.for_expected - matcher.matches?(5).should be_true + expect(matcher.matches?(5)).to be_true end it "exposes arg submitted through #new to matcher block" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do |expected| match do |actual| actual > expected end end.for_expected(4) - matcher.matches?(5).should be_true + expect(matcher.matches?(5)).to be_true end end context "with no args" do before(:each) do @@ -241,15 +241,15 @@ end end.for_expected end it "matches" do - @matcher.matches?(5).should be_true + expect(@matcher.matches?(5)).to be_true end it "describes" do - @matcher.description.should == "matcher name" + expect(@matcher.description).to eq "matcher name" end end context "with 1 arg" do before(:each) do @@ -259,15 +259,15 @@ end end.for_expected(1) end it "matches" do - @matcher.matches?(5).should be_true + expect(@matcher.matches?(5)).to be_true end it "describes" do - @matcher.description.should == "matcher name 1" + expect(@matcher.description).to eq "matcher name 1" end end context "with multiple args" do before(:each) do @@ -277,15 +277,15 @@ end end.for_expected(1,2,3,4) end it "matches" do - @matcher.matches?(10).should be_true + expect(@matcher.matches?(10)).to be_true end it "describes" do - @matcher.description.should == "matcher name 1, 2, 3, and 4" + expect(@matcher.description).to eq "matcher name 1, 2, 3, and 4" end end it "supports helper methods" do matcher = RSpec::Matchers::DSL::Matcher.new(:be_similar_to) do |sample| @@ -296,21 +296,21 @@ def similar?(a, b) a.sort == b.sort end end.for_expected([1,2,3]) - matcher.matches?([2,3,1]).should be_true + expect(matcher.matches?([2,3,1])).to be_true end it "supports fluent interface" do matcher = RSpec::Matchers::DSL::Matcher.new(:first_word) do def second_word self end end.for_expected - matcher.second_word.should == matcher + expect(matcher.second_word).to eq matcher end it "treats method missing normally for undeclared methods" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) { }.for_expected expect { matcher.non_existent_method }.to raise_error(NoMethodError) @@ -318,17 +318,55 @@ it "has access to other matchers" do matcher = RSpec::Matchers::DSL::Matcher.new(:ignore) do |expected| match do |actual| extend RSpec::Matchers - actual.should eql(5 + expected) + expect(actual).to eql(5 + expected) end end.for_expected(3) - matcher.matches?(8).should be_true + expect(matcher.matches?(8)).to be_true end + context 'when multiple instances of the same matcher are used in the same example' do + RSpec::Matchers.define(:be_like_a) do |expected| + match { |actual| actual == expected } + description { "be like a #{expected}" } + failure_message_for_should { "expected to be like a #{expected}" } + failure_message_for_should_not { "expected not to be like a #{expected}" } + end + + # Note: these bugs were only exposed when creating both instances + # first, then checking their descriptions/failure messages. + # + # That's why we eager-instantiate them here. + let!(:moose) { be_like_a("moose") } + let!(:horse) { be_like_a("horse") } + + it 'allows them to use the expected value in the description' do + expect(horse.description).to eq("be like a horse") + expect(moose.description).to eq("be like a moose") + end + + it 'allows them to use the expected value in the positive failure message' do + expect(moose.failure_message_for_should).to eq("expected to be like a moose") + expect(horse.failure_message_for_should).to eq("expected to be like a horse") + end + + it 'allows them to use the expected value in the negative failure message' do + expect(moose.failure_message_for_should_not).to eq("expected not to be like a moose") + expect(horse.failure_message_for_should_not).to eq("expected not to be like a horse") + end + + it 'allows them to match separately' do + expect("moose").to moose + expect("horse").to horse + expect("horse").not_to moose + expect("moose").not_to horse + end + end + describe "#match_unless_raises" do context "with an assertion" do let(:mod) do Module.new do def assert_equal(a,b) @@ -346,23 +384,22 @@ end.for_expected(4) end context "with passing assertion" do it "passes" do - matcher.matches?(4).should be_true + expect(matcher.matches?(4)).to be_true end end context "with failing assertion" do it "fails" do - matcher.matches?(5).should be_false + expect(matcher.matches?(5)).to be_false end it "provides the raised exception" do matcher.matches?(5) - matcher.rescued_exception.message. - should eq("5 does not equal 4") + expect(matcher.rescued_exception.message).to eq("5 does not equal 4") end end end context "with an unexpected error" do @@ -389,12 +426,12 @@ @expected_value = expected_value end match { |actual| actual == @expected_value } end.for_expected - matcher.expecting('value').matches?('value').should be_true - matcher.expecting('value').matches?('other value').should be_false + expect(matcher.expecting('value').matches?('value')).to be_true + expect(matcher.expecting('value').matches?('other value')).to be_false end it "prevents name collisions on chainable methods from different matchers" do m1 = RSpec::Matchers::DSL::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }.for_expected m2 = RSpec::Matchers::DSL::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }.for_expected @@ -412,21 +449,21 @@ RSpec::Matchers.define(:__access_running_example) do match do |actual| a_method_in_the_example == "method defined in the example" end end - example.should __access_running_example + expect(example).to __access_running_example end it "raises NoMethodError for methods not in the running_example" do RSpec::Matchers.define(:__raise_no_method_error) do match do |actual| a_method_not_in_the_example == "method defined in the example" end end expect do - example.should __raise_no_method_error + expect(example).to __raise_no_method_error end.to raise_error(/RSpec::Matchers::DSL::Matcher/) end end end