我的测试如下所示:
<<< 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我可以成功地运行rspec spec。但是autotest失败了:
Exception encountered: #<ArgumentError: Shared example group 'a user' already exists>这是因为autotest生成的rspec命令行包含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>当我从命令行中删除'/path/to/project/spec/models/user_shared.rb'并手动执行它时,它可以正常工作。
现在,如果我将我的user_shared.rb更改为:
<<< spec/models/user_shared.rb
if !@included then
@included = true
shared_examples_for "a user" do
end
end它也适用于autotest生成的命令行。但这是一个丑陋的变通方法。
既然rspec只知道测试"*_spec“文件,那么autotest怎么能这样配置呢?
在我的Gemfile中,我有以下内容(与测试相关):
<<<< 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发布于 2011-09-01 00:01:41
我自己拿的..。文件夹结构的重组。
spec/shared/并移动了其中的所有示例require *_shared.rb Dir[Rails.root.join("spec/shared/**/*.rb")].each {|f| require f}:<代码>H212<代码>F213。
<<< .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
}https://stackoverflow.com/questions/7233632
复制相似问题