Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment