Created
September 20, 2010 21:51
-
-
Save txus/588711 to your computer and use it in GitHub Desktop.
This file contains 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
# lib/rspec/core/subject.rb | |
require 'ostruct' | |
def its(attribute, &block) | |
describe(attribute) do | |
example do | |
self.class.class_eval do | |
define_method(:subject) do | |
attribute.to_s.split('.').inject(super()) do |target, method| | |
target = OpenStruct.new(target) if target.is_a?(Hash) && attribute.is_a?(Array) | |
target.send(method) | |
end | |
end | |
end | |
instance_eval(&block) | |
end | |
end | |
end | |
# spec/rspec/core/example_group_spec.rb | |
context "when it's a Hash" do | |
subject do | |
{ :attribute => 'value', | |
'another_attribute' => 'another_value'} | |
end | |
its([:attribute]) { should == 'value' } | |
its([:another_attribute]) { should == 'another_value' } | |
its(:keys) { should == ['another_attribute', :attribute] } | |
context "when referring to an attribute without the proper array syntax" do | |
it "raises a NoMethodError" do | |
expect{ its(:attribute) }.to raise_error(NoMethodError) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment