Skip to content

Instantly share code, notes, and snippets.

@hobodave
Created October 24, 2014 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hobodave/018ea4e2c09771b6e800 to your computer and use it in GitHub Desktop.
Save hobodave/018ea4e2c09771b6e800 to your computer and use it in GitHub Desktop.
RSpec::Matchers.define :included_nested do |*expected|
match do |actual|
include_nested(actual, expected)
end
def include_nested(actual, expected)
expected.inject(actual) do |acc, ex|
return false unless RSpec::Matchers::BuiltIn::Include.new(ex).matches?(acc)
acc[ex]
end
true
end
failure_message_for_should do |actual|
"expected that #{actual} would include [#{expected.map(&:inspect).join('][')}]"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not include [#{expected.map(&:inspect).join('][')}]"
end
description do
"include [#{expected.map(&:inspect).join('][')}]"
end
end
subject do
{
'goods_detail' => {
'expedited_shipping_cost' => 1.2
}
}
end
# Pass
it { should included_nested('goods_detail', 'expedited_shipping_cost') }
# Fail
# expected that {"goods_detail"=>{"expedited_shipping_cost"=>1.2}} would include ["goods_detail"]["expedited_shipping_costx"][:foo]
it { should included_nested('goods_detail', 'expedited_shipping_costx', :foo) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment