On Dec 13, 2011, at 6:30 PM, Lille wrote:
I moved your post to the bottom. Please read
Top Posting and Bottom Posting and please bottom post or inline post
so readers can more easily follow the thread.
shared examples in my dependent gem’s spec_helper?
before { … }
end
seehttps://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared…
seehttp://rubydoc.info/github/rspec/rspec-core/master/RSpec/Core/SharedC…
HTH,
David
Thank you.
Here is what I did to conform my gem spec to the conventions suggested
at
https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples…
my spec/spec_helper.rb…
$LOAD_PATH.unshift(File.dirname(FILE))
$LOAD_PATH.unshift(File.join(File.dirname(FILE), ‘…’, ‘lib’))
You don’t need to modify the ^^ LOAD_PATH ^^ here - it’s already done
for you by RSpec.
Dir[“spec/support/**/*.rb”].each {|f| require f} # this did not work…
I’m assuming you’re using Ruby 1.9, which does not include “.” on the
LOAD_PATH (which is assumed by Dir[“spec/support/**/*.rb”]). Either of
these would work:
Dir[“./spec/support//*.rb"].each {|f| require f}
Dir["support//*.rb”].each {|f| require f}
I’ll update the docs on relishapp.com accordingly.
require File.dirname(FILE) + “/…/spec/support/shared_examples.rb”
This ^^ is going to get you into trouble because Ruby doesn’t see
/path/to/x/…/spec/support/shared_examples.rb and
/path/to/spec/support/shared_examples.rb as the same path (even though
they end at the same file). This means that your users will likely end
up requiring shared_examples.rb twice, which makes bad things happen.
I’d recommend modifying and relying on the LOAD_PATH instead. More
below.
finally, here are the top lines of the spec/support/
shared_sample.rb…
extend RSpec::Core::SharedContext
shared_examples_for “Rum92265::A3::PartB” do
You don’t need both “extend RSpec::Core::SharedContext” and
“shared_examples_for”. Just one or the other.
Anyway, I hadn’t seen any gems with shared example groups exposed, so
this was the best I could come up with – it worked. It seems weird to
require shared_samples.rb from the lib directory, but I couldn’t see
where else – gemspec, for example? – to require it.
Lille
If the idea here is to expose shared_examples to users of your gem, I’d
actually store them under lib. Something like:
lib/rum_92265/spec_support.rb # requires
rum_92265/spec_support/shared_examples.rb
lib/rum_92265/spec_support/shared_examples.rb
Now you can require ‘rum_92265/spec_support’ in your own
spec/spec_helper.rb, and you can tell your users to do the same.
HTH,
David