3

私のテストは次のようになります。

<<< 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
4

1 に答える 1

2

自分で手に入れました...フォルダ構造の再編成。

  • 新しいフォルダspec/shared/を作成し、そこにすべての例を移動しました
  • require *_shared.rb私の例から削除しました
  • Dir[Rails.root.join("spec/shared/**/*.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
  }
于 2011-08-31T16:01:41.353 に答える