19704


rspec2.99でinclude_examplesを使用しています。  
spechoge.rb

[code]

    describe 'hoge_type' do
      before do
      end
      include_examples 'toarutesuto'
    end
    
[/code]

spec/support/lib/kyouyuu.rb

[code]

    require 'spec_helper'
    
    describe "テストを共有" do
      before do
      end
      shared_examples 'toarutesuto' do
        before do
        end
        it'' do
        end
    
[/code]

テストを実行すると、以下ワーニングが出ます。

[code]

    Deprecation Warnings:
    
    --------------------------------------------------------------------------------
                   Accessing shared_examples defined across contexts is deprecated.
                   Please declare shared_examples within a shared context, or at the top level.
                   This message was generated at: /poge/hoge/workspace/transpec/spechoge.rb:38:in `block (2 levels) in <top (required)>'
    --------------------------------------------------------------------------------
    
[/code]

[説明を](http://rspec.info/blog/2013/07/rspec-2-14-is-
released/)読んでみたのですが、shared_examplesは3.0からは使えなくなって直接spechoge.rb等に書かなければならないということでしょうか？  
すみません、英語がきちんと理解できていなくて

shared_examples⇒shared_examples_for  
include_examples⇒it_behaves_like  
に書き直しても同じワーニングが出ます。

回答をいただいた通りdescribeを⇒shared_contextに書き換えてみました。そうすると以下のようなエラーが出ました。rspecのバージョンを上げる必要があるのでしょうか？

[code]

    No DRb server is running. Running in local process instead ...
    /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:266:in `find_and_eval_shared': Could not find shared examples "toarutesuto" (ArgumentError)
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:185:in `block in it_behaves_like'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `module_eval'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `subclass'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:342:in `describe'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:184:in `it_behaves_like'
            from /poge/hoge/workspace/transpec4.0/spec/lib/cloudstack/os_spec.rb:38:in `block (2 levels) in <top (required)>'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `module_eval'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `subclass'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:342:in `describe'
            from /poge/hoge/workspace/transpec4.0/spec/lib/cloudstack/os_spec.rb:32:in `block in <top (required)>'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `module_eval'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:369:in `subclass'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/example_group.rb:342:in `describe'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/dsl.rb:18:in `describe'
            from /poge/hoge/workspace/transpec4.0/spec/lib/cloudstack/os_spec.rb:20:in `<top (required)>'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `load'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `block in load_spec_files'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `each'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/configuration.rb:1065:in `load_spec_files'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:18:in `run'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:100:in `rescue in run'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:96:in `run'
            from /poge/hoge/.rbenv/versions/2.2.3/gemsets/4.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:in `block in autorun'
    
[/code]


CC BY-SA 3.0
2015-12-07T07:33:59.440
19701
2015-12-07T09:06:12.937
2015-12-07T09:06:12.937
10851
10851
question
ruby-on-rails ruby rspec
shared_examplesはrspec3.0から使えない？


警告を訳すと以下のようになります。

> contextをまたがってshared_examplesにアクセスするのは非推奨になりました。  
>  shared_contextの中、もしくはトップレベルにshared_examplesを定義してください。

「contextをまたがる」というのは「describeをまたがる」と読み替えた方がわかりやすいかもしれません。  
（そのままだと同じdescribeブロック内でしかshared_examplesが使い回せない、ということです）

「shared_contextの中、もしくはトップレベルにshared_examplesを定義してください」とあるのでそれが解決策になります。  
この場合、kyouyuu.rbが別ファイルになっているので、shared_contextを使うのが良さそうです。

[code]

    # spec/support/lib/kyouyuu.rb
    require 'spec_helper'
    
    shared_context "テストを共有" do
      before do
      end
      shared_examples 'toarutesuto' do
        before do
        end
        it'' do
        end
    
    # spechoge.rb
    describe 'hoge_type' do
      include_context 'テストを共有'
      before do
      end
      it_behaves_like 'toarutesuto'
    end
    
[/code]

このようにshared_context内にshared_examplesを定義して、必要なテストでinclude_contextすればshared_examplesが別のファイルでも使えるようになります。


CC BY-SA 3.0
2015-12-07T08:41:41.417
19704
2015-12-07T08:41:41.417
85
19701
answer