My tests look like this:
<<< spec/models/user_shared.rb
shared_examples_for "a user" do
end
<<< spec/models/worker_spec.rb
require 'spec_helper'
require 'models/user_shared'
describe Worker do
it_behaves_like "a user"
end
I can run rspec spec
successfully. But autotest
fails:
Exception encountered: #<ArgumentError: 开发者_开发百科Shared example group 'a user' already exists>
This is, because the rspec command line generated by autotest
includes the user_shared.rb
:
/usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby -rrubygems -S /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/bin/rspec --tty [...] '/path/to/project/spec/models/worker_spec.rb' '/path/to/project/spec/models/user_shared.rb'
Running tests with args ["--color", "--tty", [...], "/path/to/project/spec/models/worker_spec.rb", "/path/to/project/spec/models/user_shared.rb"]...
Done.
Exception encountered: #<ArgumentError: Shared example group 'a user' already exists>
When I remove the '/path/to/project/spec/models/user_shared.rb'
from the command line and execute it by hand, it works.
Now, if I change my user_shared.rb
to this:
<<< spec/models/user_shared.rb
if !@included then
@included = true
shared_examples_for "a user" do
end
end
it works with the command line generated by autotest
, too. But it is an ugly workaround.
Since rspec
knows only "*_spec" files are testes, how can autotest
be configured like this?
In my Gemfile I have the following (relevant to testing):
<<<< Gemfile
gem 'autotest'
gem 'autotest-rails'
group :test do
gem 'rspec-rails', '>= 2.6.1'
gem 'shoulda-matchers'
gem 'factory_girl_rails', '>= 1.0.2'
gem 'capybara', '>= 1.0.0'
gem 'cucumber-rails', '>= 1.0.2'
gem 'database_cleaner', '>= 0.6.7'
gem 'spork', '>= 0.9.0.rc'
end
Got it myself ... reorganization of folder structure.
- created new folder
spec/shared/
and moved all examples in there - removed any
require *_shared.rb
from my examples - added
Dir[Rails.root.join("spec/shared/**/*.rb")].each {|f| require f}
tospec_helper.rb
- tweaked autotest:
.
<<< .autotest
at.add_mapping(%r%^spec/shared/.*rb%) { |_, _|
Dir['spec/**/*_spec.rb'] # could be tweaked to just run tests that have this example
# but for now I don't care - just run all tests
}
精彩评论