Guard-Rspec Shared Example Groups

45 views
Skip to first unread message

Andy L

unread,
Jun 5, 2014, 4:11:03 PM6/5/14
to guar...@googlegroups.com
My test suite has shared_examples that are included in many spec files.

When I edit my shared_example file, what is the best way to auto-run all of the specs that include the shared example?

Andy L

unread,
Jun 5, 2014, 10:28:31 PM6/5/14
to guar...@googlegroups.com
FYI here is how I solved the problem:

1. adopted a file naming convention - shared examples go in *_shared.rb
2. comment goes in the *_shared.rb file - eg "#required_by: model/file1 model/file2"
3. a watcher triggers off the shared file and calls a method that generates a list of consumers

watch(%r{^spec/(.+)_shared?\.rb$})  { |m| consumers_for(m[0]) }

4. here is the method that parses the shared file and generates a list of consumers:

def consumers_for(share_file)
  tgts = []
  File.open(share_file).each do |line|
    if %r{^#required_by:(.*)$}.match(line)
      tgts += $1.split
    end
  end
  tgts.map {|tgt| "spec/#{tgt}_spec.rb"}.sort.uniq
end

Cezary Baginski

unread,
Dec 21, 2014, 1:35:12 PM12/21/14
to guar...@googlegroups.com
I've wanted a feature like this for months - for RSpec to automatically match files if there's no other match.

I added a feature request here in case you want to watch it: https://github.com/guard/guard-rspec/issues/307
Reply all
Reply to author
Forward
0 new messages