RSpec:为真实对象使用新的allow / expect样式

时间:2013-07-30 17:01:17

标签: ruby rspec rspec2 rspec-rails

在RSpec中,我试图在我的Rails视图规范中转换以下视图存根:

view.stub(:current_page?).with(root_url).and_return(true)

要使用新的allow / expect语法,但以下内容不起作用:

allow(view).to receive(:current_page?).with(root_url).and_return(true)

然而,这给了我一个 NoMethodError

 NoMethodError:
   undefined method `allow' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1::Nested_2:0x007fc7ca971988>

RDocs表明allow应该接受双重或真实对象。这是一个错误还是我做错了什么?非常感谢!

以下是规范的源代码:

require "spec_helper"

describe 'Navigation Bar' do

  # This needs to be kept synced up with the links in the navigation bar
  KNOWN_LINKS = [:deal_gallery, :list, :login, :logout, :edit_account, :signup,
                 :categories]

  shared_examples "Navigation Bar Link Context" do |params|

    expected_links = params[:expected_links]

    context 'Outside Deal Gallery' do

      before(:each) { render partial: "layouts/navigation", formats: [:html] }

      it_has_only_links_with_ids
      it_has_only_known_links KNOWN_LINKS
      it_has_links expected_links
      it_does_not_have_links KNOWN_LINKS - expected_links

    end

    context 'On Deal Gallery' do

      before(:each) do
        # view.stub(:current_page?).with(root_url).and_return(true)
        allow(view).to receive(:current_page?).with(root_url).and_return(true)
        render partial: "layouts/navigation", formats: [:html]
      end

      it_has_only_links_with_ids
      it_has_only_known_links KNOWN_LINKS
      it_has_link :categories
      it_does_not_have_link :deal_gallery
    end

  end

  context 'For Anonymous User' do

    it_behaves_like "Navigation Bar Link Context",
      expected_links: [:deal_gallery, :login, :signup]

  end

  context 'For Authenticated User' do

    login_user

    it_behaves_like "Navigation Bar Link Context",
      expected_links: [:deal_gallery, :list, :logout, :edit_account]

  end

end

0 个答案:

没有答案