0

我在我的 Rails 应用程序上使用 rspec,并更新了我的 gem。现在,当我运行测试时,我收到以下消息:

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: /home/flo/RoR/letroquet/spec/requests/user_show_page_spec.rb:50:in `block (3 levels) in <top (required)>'

这是user_show_page_spec.rb

describe "show user page" do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  let(:current_page) { user_path(user) }

  describe "aside info" do

    describe "when not signed-in" do
      before { visit current_page }

      it { should have_selector('h1', text: user.name) }

      it_should_behave_like "another user products count" # This is the 50th line
    end
  end
end

这里是spec/support/users/info.rb

require 'spec_helper'

describe "products count" do
  subject { page }

  shared_examples_for "another user products count" do
    before { visit current_page }

    it { should have_selector('b', text: t('product.owned.count', count: user.transactions.current.ownership.count)) }
    it { should have_selector('li', text: t('product.givable.count', count: user.products.owned.givable.count)) }
    it { should have_selector('li', text: t('product.sharable.count', count: user.products.owned.sharable.count)) }
    it { should have_selector('li', text: t('product.borrowed.count', count: user.products.borrowed.count)) }

    it { should have_selector('li', text: t('product.given.count', count: user.products.given.count)) }
    it { should have_selector('li', text: t('product.returned.count', count: user.products.returned.count)) }

  end
end

你能告诉我我必须如何更改我的测试文件吗?

4

1 回答 1

1

This subject is discussed in http://myronmars.to/n/dev-blog/2013/07/rspec-2-14-is-released and examples given in https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples

Per that documentation, you need to place the examples within the scope of their use by direct inclusion or the use of 'require'. In any event, note that the shared_examples_for invocation needs to be within the scope, it can't be buried within a describe as is currently the case with the code in info.rb.

So, assuming that your spec_helper.rb file is already automatically including the files in spec/support, you could change your info.rb file to be:

shared_examples_for "another user products count" do
  subject { page }
  before { visit current_page }

  it { should have_selector('b', text: t('product.owned.count', count: user.transactions.current.ownership.count)) }
  it { should have_selector('li', text: t('product.givable.count', count: user.products.owned.givable.count)) }
  it { should have_selector('li', text: t('product.sharable.count', count: user.products.owned.sharable.count)) }
  it { should have_selector('li', text: t('product.borrowed.count', count: user.products.borrowed.count)) }

  it { should have_selector('li', text: t('product.given.count', count: user.products.given.count)) }
  it { should have_selector('li', text: t('product.returned.count', count: user.products.returned.count)) }

end

and it should work. Note that the subject call was moved within the shared example block.

于 2013-09-16T13:22:21.550 回答