El 23/07/2010, a las 09:09, Ashley Moran escribió:
> I'm looking for the best way to parameterise shared examples. Imagine (as an
> academic example...) you were doing it for subclasses of Struct instances (a
> more realistic example might be ActiveRecord subclasses, or DataMapper
> resources), such as:
>
> class MyStruct < Struct.new(:a, :b)
> end
>
> class MyOtherStruct < Struct.new(:foo, :bar)
> end
>
>
> I've seen it done with #let, eg:
>
> shared_examples_for "a Struct" do
> it "has methods" do
> properties.each do |property|
> struct.should respond_to(property)
> end
> end
> end
>
> describe MyStruct do
> let(:struct) { MyStruct.new }
> let(:properties) { [:a, :b] }
> it_should_behave_like "a Struct"
> end
>
> describe MyOtherStruct do
> let(:struct) { MyOtherStruct.new }
> let(:properties) { [:foo, :bar] }
> it_should_behave_like "a Struct"
> end
>
> Which is not a bad solution, but does feel a bit too much like using (scoped)
> global variables for my liking. There's no explicit association between the
> shared examples and their parameters (and the arguments actually passed in
> each example group.
Recently commited (RSpec 2.0.0.beta.18) was the ability to pass a block to
"it_should_behave_like", making the relation clearer; eg:
describe MyStruct do
it_should_behave_like 'a Struct' do
let(:struct) { MyStruct.new }
end
end
I did ask about parametrizing that explicitly via metadata, but David feels
that the block based approach is better; see the full thread here:
http://github.com/rspec/rspec-core/issues/71
Wincent
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users