自动测试不适用于 rspec 共享示例

发布于 2024-12-02 03:02:41 字数 1852 浏览 3 评论 0原文

我的测试如下所示:

<<< 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-12-09 03:02:41

我自己得到了......文件夹结构的重组。

  • 创建了新文件夹 spec/shared/ 并将所有示例移至其中,
  • 从我添加的示例中删除了所有 require *_shared.rb
  • Dir[Rails.root.join("规格/共享/**/*.rb")].each {|f| require f}spec_helper.rb
  • 调整了自动测试:

<<< .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
  }

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} to spec_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
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文