Skip to content

Commit

Permalink
update a couple of specs to more 21st century syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Mar 28, 2011
1 parent 20e9601 commit 82c5d66
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 168 deletions.
91 changes: 45 additions & 46 deletions spec/rspec/mocks/failing_argument_matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,92 @@ module RSpec
module Mocks
describe "failing MockArgumentMatchers" do
before(:each) do
@mock = double("test double")
@reporter = RSpec::Mocks::Mock.new("reporter").as_null_object
@double = double("double")
@reporter = double("reporter").as_null_object
end

after(:each) do
@mock.rspec_reset
@double.rspec_reset
end

it "rejects non boolean" do
@mock.should_receive(:random_call).with(boolean())
lambda do
@mock.random_call("false")
end.should raise_error(RSpec::Mocks::MockExpectationError)
@double.should_receive(:random_call).with(boolean())
expect do
@double.random_call("false")
end.to raise_error(RSpec::Mocks::MockExpectationError)
end

it "rejects non numeric" do
@mock.should_receive(:random_call).with(an_instance_of(Numeric))
lambda do
@mock.random_call("1")
end.should raise_error(RSpec::Mocks::MockExpectationError)
@double.should_receive(:random_call).with(an_instance_of(Numeric))
expect do
@double.random_call("1")
end.to raise_error(RSpec::Mocks::MockExpectationError)
end

it "rejects non string" do
@mock.should_receive(:random_call).with(an_instance_of(String))
lambda do
@mock.random_call(123)
end.should raise_error(RSpec::Mocks::MockExpectationError)
@double.should_receive(:random_call).with(an_instance_of(String))
expect do
@double.random_call(123)
end.to raise_error(RSpec::Mocks::MockExpectationError)
end

it "rejects goose when expecting a duck" do
@mock.should_receive(:random_call).with(duck_type(:abs, :div))
lambda { @mock.random_call("I don't respond to :abs or :div") }.should raise_error(RSpec::Mocks::MockExpectationError)
@double.should_receive(:random_call).with(duck_type(:abs, :div))
expect { @double.random_call("I don't respond to :abs or :div") }.to raise_error(RSpec::Mocks::MockExpectationError)
end

it "fails if regexp does not match submitted string" do
@mock.should_receive(:random_call).with(/bcd/)
lambda { @mock.random_call("abc") }.should raise_error(RSpec::Mocks::MockExpectationError)
@double.should_receive(:random_call).with(/bcd/)
expect { @double.random_call("abc") }.to raise_error(RSpec::Mocks::MockExpectationError)
end

it "fails if regexp does not match submitted regexp" do
@mock.should_receive(:random_call).with(/bcd/)
lambda { @mock.random_call(/bcde/) }.should raise_error(RSpec::Mocks::MockExpectationError)
@double.should_receive(:random_call).with(/bcd/)
expect { @double.random_call(/bcde/) }.to raise_error(RSpec::Mocks::MockExpectationError)
end

it "fails for a hash w/ wrong values" do
@mock.should_receive(:random_call).with(:a => "b", :c => "d")
lambda do
@mock.random_call(:a => "b", :c => "e")
end.should raise_error(RSpec::Mocks::MockExpectationError, /Double "test double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(:a=>\"b\", :c=>\"e\"|:c=>\"e\", :a=>\"b\")\}\)/)
@double.should_receive(:random_call).with(:a => "b", :c => "d")
expect do
@double.random_call(:a => "b", :c => "e")
end.to raise_error(RSpec::Mocks::MockExpectationError, /Double "double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(:a=>\"b\", :c=>\"e\"|:c=>\"e\", :a=>\"b\")\}\)/)
end

it "fails for a hash w/ wrong keys" do
@mock.should_receive(:random_call).with(:a => "b", :c => "d")
lambda do
@mock.random_call("a" => "b", "c" => "d")
end.should raise_error(RSpec::Mocks::MockExpectationError, /Double "test double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(\"a\"=>\"b\", \"c\"=>\"d\"|\"c\"=>\"d\", \"a\"=>\"b\")\}\)/)
@double.should_receive(:random_call).with(:a => "b", :c => "d")
expect do
@double.random_call("a" => "b", "c" => "d")
end.to raise_error(RSpec::Mocks::MockExpectationError, /Double "double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(\"a\"=>\"b\", \"c\"=>\"d\"|\"c\"=>\"d\", \"a\"=>\"b\")\}\)/)
end

it "matches against a Matcher" do
lambda do
@mock.should_receive(:msg).with(equal(3))
@mock.msg(37)
end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :msg with unexpected arguments\n expected: (equal 3)\n got: (37)")
expect do
@double.should_receive(:msg).with(equal(3))
@double.msg(37)
end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n expected: (equal 3)\n got: (37)")
end

it "fails no_args with one arg" do
lambda do
@mock.should_receive(:msg).with(no_args)
@mock.msg(37)
end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :msg with unexpected arguments\n expected: (no args)\n got: (37)")
expect do
@double.should_receive(:msg).with(no_args)
@double.msg(37)
end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n expected: (no args)\n got: (37)")
end

it "fails hash_including with missing key" do
lambda do
@mock.should_receive(:msg).with(hash_including(:a => 1))
@mock.msg({})
end.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :msg with unexpected arguments\n expected: (hash_including(:a=>1))\n got: ({})")
expect do
@double.should_receive(:msg).with(hash_including(:a => 1))
@double.msg({})
end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n expected: (hash_including(:a=>1))\n got: ({})")
end

it "fails with block matchers" do
lambda do
@mock.should_receive(:msg).with {|arg| arg.should == :received }
@mock.msg :no_msg_for_you
end.should raise_error(RSpec::Expectations::ExpectationNotMetError, /expected: :received.*\s*.*got: :no_msg_for_you/)
expect do
@double.should_receive(:msg).with {|arg| arg.should == :received }
@double.msg :no_msg_for_you
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected: :received.*\s*.*got: :no_msg_for_you/)
end

end
end
end

234 changes: 112 additions & 122 deletions spec/rspec/mocks/passing_argument_matchers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,144 +1,134 @@
require 'spec_helper'

def include_mock_argument_matchers
before(:each) do
@mock = RSpec::Mocks::Mock.new("test mock")
Kernel.stub(:warn)
end

after(:each) do
@mock.rspec_verify
end
end
module RSpec
module Mocks

describe Methods, "handling argument matchers" do
include_mock_argument_matchers

it "accepts true as boolean()" do
@mock.should_receive(:random_call).with(boolean())
@mock.random_call(true)
describe Methods do
before(:each) do
@double = double('double')
Kernel.stub(:warn)
end

it "accepts false as boolean()" do
@mock.should_receive(:random_call).with(boolean())
@mock.random_call(false)
after(:each) do
@double.rspec_verify
end

it "accepts fixnum as kind_of(Numeric)" do
@mock.should_receive(:random_call).with(kind_of(Numeric))
@mock.random_call(1)
end
context "handling argument matchers" do
it "accepts true as boolean()" do
@double.should_receive(:random_call).with(boolean())
@double.random_call(true)
end

it "accepts float as an_instance_of(Numeric)" do
@mock.should_receive(:random_call).with(kind_of(Numeric))
@mock.random_call(1.5)
end

it "accepts fixnum as instance_of(Fixnum)" do
@mock.should_receive(:random_call).with(instance_of(Fixnum))
@mock.random_call(1)
end
it "accepts false as boolean()" do
@double.should_receive(:random_call).with(boolean())
@double.random_call(false)
end

it "does NOT accept fixnum as instance_of(Numeric)" do
@mock.should_not_receive(:random_call).with(instance_of(Numeric))
@mock.random_call(1)
end
it "accepts fixnum as kind_of(Numeric)" do
@double.should_receive(:random_call).with(kind_of(Numeric))
@double.random_call(1)
end

it "does NOT accept float as instance_of(Numeric)" do
@mock.should_not_receive(:random_call).with(instance_of(Numeric))
@mock.random_call(1.5)
end
it "accepts float as an_instance_of(Numeric)" do
@double.should_receive(:random_call).with(kind_of(Numeric))
@double.random_call(1.5)
end

it "accepts string as anything()" do
@mock.should_receive(:random_call).with("a", anything(), "c")
@mock.random_call("a", "whatever", "c")
end
it "accepts fixnum as instance_of(Fixnum)" do
@double.should_receive(:random_call).with(instance_of(Fixnum))
@double.random_call(1)
end

it "matches duck type with one method" do
@mock.should_receive(:random_call).with(duck_type(:length))
@mock.random_call([])
end
it "does NOT accept fixnum as instance_of(Numeric)" do
@double.should_not_receive(:random_call).with(instance_of(Numeric))
@double.random_call(1)
end

it "matches duck type with two methods" do
@mock.should_receive(:random_call).with(duck_type(:abs, :div))
@mock.random_call(1)
end

it "matches no args against any_args()" do
@mock.should_receive(:random_call).with(any_args)
@mock.random_call()
end

it "matches one arg against any_args()" do
@mock.should_receive(:random_call).with(any_args)
@mock.random_call("a string")
end

it "matches no args against no_args()" do
@mock.should_receive(:random_call).with(no_args)
@mock.random_call()
end

it "matches hash with hash_including same hash" do
@mock.should_receive(:random_call).with(hash_including(:a => 1))
@mock.random_call(:a => 1)
end

end
it "does NOT accept float as instance_of(Numeric)" do
@double.should_not_receive(:random_call).with(instance_of(Numeric))
@double.random_call(1.5)
end

describe Methods, "handling block matchers" do
include_mock_argument_matchers

it "matches arguments against RSpec expectations" do
@mock.should_receive(:random_call).with {|arg1, arg2, arr, *rest|
arg1.should == 5
arg2.should have_at_least(3).characters
arg2.should have_at_most(10).characters
arr.map {|i| i * 2}.should == [2,4,6]
rest.should == [:fee, "fi", 4]
}
@mock.random_call 5, "hello", [1,2,3], :fee, "fi", 4
end
end

describe Methods, "handling non-matcher arguments" do

before(:each) do
@mock = RSpec::Mocks::Mock.new("test mock")
end

it "matches non special symbol (can be removed when deprecated symbols are removed)" do
@mock.should_receive(:random_call).with(:some_symbol)
@mock.random_call(:some_symbol)
end
it "accepts string as anything()" do
@double.should_receive(:random_call).with("a", anything(), "c")
@double.random_call("a", "whatever", "c")
end

it "matches string against regexp" do
@mock.should_receive(:random_call).with(/bcd/)
@mock.random_call("abcde")
end
it "matches duck type with one method" do
@double.should_receive(:random_call).with(duck_type(:length))
@double.random_call([])
end

it "matches regexp against regexp" do
@mock.should_receive(:random_call).with(/bcd/)
@mock.random_call(/bcd/)
end

it "matches against a hash submitted and received by value" do
@mock.should_receive(:random_call).with(:a => "a", :b => "b")
@mock.random_call(:a => "a", :b => "b")
it "matches duck type with two methods" do
@double.should_receive(:random_call).with(duck_type(:abs, :div))
@double.random_call(1)
end

it "matches no args against any_args()" do
@double.should_receive(:random_call).with(any_args)
@double.random_call()
end

it "matches one arg against any_args()" do
@double.should_receive(:random_call).with(any_args)
@double.random_call("a string")
end

it "matches no args against no_args()" do
@double.should_receive(:random_call).with(no_args)
@double.random_call()
end

it "matches hash with hash_including same hash" do
@double.should_receive(:random_call).with(hash_including(:a => 1))
@double.random_call(:a => 1)
end
end

it "matches against a hash submitted by reference and received by value" do
opts = {:a => "a", :b => "b"}
@mock.should_receive(:random_call).with(opts)
@mock.random_call(:a => "a", :b => "b")

context "handling block matchers" do
it "matches arguments against RSpec expectations" do
@double.should_receive(:random_call).with {|arg1, arg2, arr, *rest|
arg1.should == 5
arg2.should have_at_least(3).characters
arg2.should have_at_most(10).characters
arr.map {|i| i * 2}.should == [2,4,6]
rest.should == [:fee, "fi", 4]
}
@double.random_call 5, "hello", [1,2,3], :fee, "fi", 4
end
end

it "matches against a hash submitted by value and received by reference" do
opts = {:a => "a", :b => "b"}
@mock.should_receive(:random_call).with(:a => "a", :b => "b")
@mock.random_call(opts)

context "handling non-matcher arguments" do
it "matches non special symbol (can be removed when deprecated symbols are removed)" do
@double.should_receive(:random_call).with(:some_symbol)
@double.random_call(:some_symbol)
end

it "matches string against regexp" do
@double.should_receive(:random_call).with(/bcd/)
@double.random_call("abcde")
end

it "matches regexp against regexp" do
@double.should_receive(:random_call).with(/bcd/)
@double.random_call(/bcd/)
end

it "matches against a hash submitted and received by value" do
@double.should_receive(:random_call).with(:a => "a", :b => "b")
@double.random_call(:a => "a", :b => "b")
end

it "matches against a hash submitted by reference and received by value" do
opts = {:a => "a", :b => "b"}
@double.should_receive(:random_call).with(opts)
@double.random_call(:a => "a", :b => "b")
end

it "matches against a hash submitted by value and received by reference" do
opts = {:a => "a", :b => "b"}
@double.should_receive(:random_call).with(:a => "a", :b => "b")
@double.random_call(opts)
end
end
end
end
Expand Down

0 comments on commit 82c5d66

Please sign in to comment.