Limitations of it_behaves_like
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 66
- Forks
- 66
- PR merge metrics
- No merged PRs in 30d
Description
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
-
All it_behaves_like calls must have an identical
@methodand@objectcalls within a given scope, or they will affect each others' state. To overcome this, you have to wrap the differing parameter in acontextordescribeblock. -
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
@methodhas shared scope. To get around this, I have saved the outer@methodinto a new@base_methodinside a before and referred to it via@base_methodto 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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the implementation of it_behaves_like and the shared-spec examples related to ruby/spec#576. Reproduce cases with differing method and object arguments, including nested shared specs, then determine whether each behaves_like invocation keeps independent bindings. Done means those cases no longer affect one another and the relevant tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100