我正在尝试测试以下代码:
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/