Skip to content

Limitations of it_behaves_like #36

@cadwallion

Description

@cadwallion

While implementing shared specs for ruby/spec#576, I noticed some limitations in the implementation of it_behaves_like that should be addressed.

Issues Discovered

  1. All it_behaves_like calls must have an identical @method and @object calls within a given scope, or they will affect each others' state. To overcome this, you have to wrap the differing parameter in a context or describe block.

  2. If you have a shared spec that relies upon another shared spec, the above issue gets more complex, because even if you've wrapped it in a describe block, the @method has shared scope. To get around this, I have saved the outer @method into a new @base_method inside a before and referred to it via @base_method to solve the collision.

Proposed Solution

Instead of mapping to @method and @object, scoping these to instance variables specific to the behaves_like description:

def it_behaves_like desc, meth, obj=nil
  send :before, :all do
    @shared_bindings ||= {}
    @shared_bindings[desc] = { method: meth, object: obj }
  end
end

I haven't fully fleshed this out, but if it sounds like a reasonable approach I can go further on a fleshed out solution and PR accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions