params
在测试Rails助手方法时设置哈希值?我想设置params[:search] = 'my keyword search'
在我的辅助方法中使用,然后从it
示例块中调用它。
require 'rails_helper'
describe BooksHelper do
describe "#page_title_helper" do
let(:params) { {search: 'my keyword search'} }
it "should read the params hash" do
expect(helper.params[:search]).to eq "my keyword search"
end
end
end
BooksHelper
def title_helper
if params[:search]
"Books related to #{params[:search]}"
else
"All Books"
end
end
end
在RSpec 3中,params哈希在控制器对象上可用,该对象在帮助程序规范中可用。因此,例如,在得到params[:search]
,说controller.params[:search]
。
这是从问题扩展而来的扩展示例。
context "with a params[:search]" do
it "returns the search term" do
controller.params[:search] = 'Test Search'
expect(helper.title_helper).to eq("Books related to #{params[:search]}".html_safe)
expect(helper.title_helper).to eq("Books related to Test Search".html_safe)
expect(helper.title_helper).not_to eq("Books related to Bad Value".html_safe)
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句