我的测试如下所示:
<<< 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>
这是因为生成的 rspec 命令行autotest
包括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