自动测试不适用于 rspec 共享示例
我的测试如下所示:
<<< 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己得到了......文件夹结构的重组。
spec/shared/
并将所有示例移至其中,require *_shared.rb
Dir[Rails.root.join("规格/共享/**/*.rb")].each {|f| require f}
到spec_helper.rb
。
Got it myself ... reorganization of folder structure.
spec/shared/
and moved all examples in thererequire *_shared.rb
from my examplesDir[Rails.root.join("spec/shared/**/*.rb")].each {|f| require f}
tospec_helper.rb
.