Created
October 24, 2014 20:57
-
-
Save hobodave/018ea4e2c09771b6e800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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