[rspec-users] Evaluating shared example customisation block before shared block

David Chelimsky dchelimsky at gmail.com
Fri Jul 30 11:47:55 EDT 2010


On Jul 30, 2010, at 10:17 AM, Wincent Colaiuta wrote:

> El 30/07/2010, a las 16:10, David Chelimsky escribió:
> 
>> Actually - maybe I spoke to soon. Ordering things this way would mean that you couldn't do this:
>> 
>> shared_examples_for Enumerable do
>> def enumerable
>>   raise "you must provide an enumerable method that returns the object which you're specifying should behave like Enumerable"
>> end
>> it "..." { .. }
>> end
>> 
>> Although, if you were going to do that, I guess you could do this:
>> 
>> shared_examples_for Enumerable do
>> unless defined?(:enumerable)
>>   raise "you must provide an enumerable method that returns the object which you're specifying should behave like Enumerable"
>> end
>> it "..." { .. }
>> end
>> 
>> Maybe that, or a DSL that wraps that, is the better way, so we can get the best of both worlds?
>> 
>> shared_examples_for Enumerable do
>> require_instance_method :foo, "gotta have foo instance method"
>> require_class_method :foo, "gotta have foo class method"
>> require_instance_variable "@foo", "gotta have an instance variable named @foo"
>> it "..." { .. }
>> end
>> 
>> Thoughts?
> 
> I think the DSL would probably be over-engineering.
> 
> One of the purposes the DSL would fulfill is to alert the user if he/she forgets to provide some required support methods or variables, but you already get those alerts "for free" in the form of failing specs and NoMethodErrors etc, so I don't think that really justifies it.
> 
> The other purpose of the DSL would be to explicitly list the "dependencies" of the shared example group to someone who's scanning it. Again, I'm not sure if it's really justified, given that a much simpler solution already exists:
> 
>  shared_examples_for Enumerable do
>    # requires:
>    #   - instance method: foo
>    #   - class method: foo
>    #   - instance variable: @foo
> 
>    it "..." { ... }
>  end
> 
> (I'm a big fan of doing the simplest thing that could possibly work.)

That would only work if people read the docs :)

The programatic approach would warn the user when they try to do something.

I'm not sold on the DSL at this point, but I do like the idea of having a library help the developer do the right thing. If I provide spec extensions for a library (other than RSpec in this case) that include a shared group that "you can use to spec your foos to make sure they comply with this API" (or whatever), then this sort of thing can be very helpful. Make sense?

> 
> Cheers,
> Wincent
> 
> 
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list