3

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

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

要使用新的允许/预期语法,但以下内容不起作用:

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
4

0 回答 0