Guest User

Untitled

a guest
Jun 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. # lib/rspec/core/subject.rb
  2.  
  3. require 'ostruct'
  4.  
  5. def its(attribute, &block)
  6. describe(attribute) do
  7. example do
  8. self.class.class_eval do
  9. define_method(:subject) do
  10. attribute.to_s.split('.').inject(super()) do |target, method|
  11. target = OpenStruct.new(target) if target.is_a?(Hash) && attribute.is_a?(Array)
  12. target.send(method)
  13. end
  14. end
  15. end
  16. instance_eval(&block)
  17. end
  18. end
  19. end
  20.  
  21. # spec/rspec/core/example_group_spec.rb
  22.  
  23. context "when it's a Hash" do
  24. subject do
  25. { :attribute => 'value',
  26. 'another_attribute' => 'another_value'}
  27. end
  28. its([:attribute]) { should == 'value' }
  29. its([:another_attribute]) { should == 'another_value' }
  30. its(:keys) { should == ['another_attribute', :attribute] }
  31.  
  32. context "when referring to an attribute without the proper array syntax" do
  33. it "raises a NoMethodError" do
  34. expect{ its(:attribute) }.to raise_error(NoMethodError)
  35. end
  36. end
  37.  
  38. end
Add Comment
Please, Sign In to add comment