Module: Its::ExampleGroupMethods
- Defined in:
- lib/its.rb
Instance Method Summary collapse
-
#its(attribute, *args, &block) ⇒ Object
Creates a nested example group named by the submitted
attribute
, and then generates an example using the submitted block.
Instance Method Details
#its(attribute, *args, &block) ⇒ Object
Creates a nested example group named by the submitted attribute
, and then generates an example using the submitted block.
# This ...
describe Array do
its(:size) { should eq(0) }
end
# ... generates the same runtime structure as this:
describe Array do
describe "size" do
it "should eq(0)" do
subject.size.should eq(0)
end
end
end
The attribute can be a Symbol
or a String
. Given a String
with dots, the result is as though you concatenated that String
onto the subject in an expression.
describe Person do
subject do
Person.new.tap do |person|
person.phone_numbers << "555-1212"
end
end
its("phone_numbers.first") { should eq("555-1212") }
end
When the subject is a Hash
, you can refer to the Hash keys by specifying a Symbol
or String
in an array.
describe "a configuration Hash" do
subject do
{ :max_users => 3,
'admin' => :all_permissions }
end
its([:max_users]) { should eq(3) }
its(['admin']) { should eq(:all_permissions) }
# You can still access to its regular methods this way:
its(:keys) { should include(:max_users) }
its(:count) { should eq(2) }
end
You can also pass any additional arguments the target method can accept:
describe Person do
subject do
Person.new.tap do |person|
person.phone_numbers << "123-123"
person.phone_numbers << "234-234"
person.phone_numbers << "xxx-xxx"
end
end
its("phone_numbers.first", 2) { should == ["123-123", "234-234"]
end
This is the extraction from the RSpec Core. For reference, see:
This modifies the following behaviour:
-
calls the target with arguments passed it
-
changes the description to include the args (if any)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/its.rb', line 76 def its(attribute, *args, &block) desc = attribute.to_s desc += "(#{args.map{|a| a.nil? ? 'nil' : a.to_s}.join(', ')})" unless args.empty? describe(desc) do example do self.class.class_eval do define_method(:subject) do @_subject ||= if attribute.is_a?(Array) super()[*attribute] else attribute.to_s.split('.').inject(super()) do |target, method| target.send(method, *args) end end end end instance_eval(&block) end end end |