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

Wincent Colaiuta win at wincent.com
Fri Jul 30 11:17:23 EDT 2010


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.)

Cheers,
Wincent





More information about the rspec-users mailing list