ruby-on-rails - RSpec Helper 参数问题

标签 ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails

我正在尝试测试以下代码:

module ApplicationHelper
  def current_book
    Book.find(params[:id])
  end
end

使用 RSpec 进行以下测试:

RSpec.describe ApplicationHelper, :type => :helper do
  describe "#current_book" do
    book_1 = create(:book)

    params = {}
    params[:id] = book_1.id

    expect(helper.current_book).to eq(book_1)
  end
end

但由于某些原因,params[:id] 参数没有正确传递。有什么建议吗?

最佳答案

您需要 stub 参数:

RSpec.describe ApplicationHelper, type: :helper do
  describe "#current_book" do
    let(:first_book) { create(:book) }

    before(:all) { helper.stub!(:params).and_return(id: 1) }

    it "returns a book with a matching id" do
      expect(helper.current_book).to eq(first_book)
    end
  end
end

关于ruby-on-rails - RSpec Helper 参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31591528/

相关文章:

css - AngularJS CSS中CSS选择器&和>是如何使用的

ruby-on-rails - 由于 sqlite gem 导致 Heroku 错误

ruby-on-rails - 使用 self 作为 ruby​​ 实例方法中的第一个参数

ruby - 搜索 XML 并将节点的子集作为 XML 获取

ruby `split' : invalid byte sequence in UTF-8 (ArgumentError)

ruby-on-rails-4 - 在产品页面上显示 Spree 分类树

ruby-on-rails - RSpec:带有 searchkick 的无效模型

ruby-on-rails - 如何在 Ruby 中使用 X509Certificate 对 xml 进行签名?

ruby - 如何让 rspec-2 给出与测试失败相关的完整跟踪?

postgresql - 如何使用 PostgreSQL 解决 Rails 中的错误 'fe_sendauth: no password supplied'?