Q:

RSpec Helper Parameters Issue

I'm trying to test the following code:
module ApplicationHelper
  def current_book
    Book.find(params[:id])
  end
end

using the following test with 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

But for some reason the params[:id] parameter isn't being passed in properly. Any suggestions with this?

A:

You need to stub the params:
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

