首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >autotest不适用于rspec共享示例

autotest不适用于rspec共享示例
EN

Stack Overflow用户
提问于 2011-08-30 01:33:39
回答 1查看 557关注 0票数 3

我的测试如下所示:

代码语言:javascript
复制
<<< 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失败了:

代码语言:javascript
复制
Exception encountered: #<ArgumentError: Shared example group 'a user' already exists>

这是因为autotest生成的rspec命令行包含user_shared.rb

代码语言:javascript
复制
/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更改为:

代码语言:javascript
复制
<<< spec/models/user_shared.rb

if !@included then
  @included = true
  shared_examples_for "a user" do
  end
end

它也适用于autotest生成的命令行。但这是一个丑陋的变通方法。

既然rspec只知道测试"*_spec“文件,那么autotest怎么能这样配置呢?

在我的Gemfile中,我有以下内容(与测试相关):

代码语言:javascript
复制
<<<< 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-01 00:01:41

我自己拿的..。文件夹结构的重组。

  • 创建了新文件夹spec/shared/并移动了其中的所有示例
  • 删除了my examples中的所有require *_shared.rb
  • 在autotest中添加了Dir[Rails.root.join("spec/shared/**/*.rb")].each {|f| require f}:<代码>H212<代码>F213

代码语言:javascript
复制
<<< .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
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7233632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档