私はレールに次のヘルパーメソッドをテストしようとしている:ヘルパーのユニットテストは、現在のパスに基づいて動作を変更しますか?
def current_has_class_link(text, path, class_name="selected")
link_to_unless_current(text, path) do
link_to(text, path, :class => class_name)
end
end
私はこのようなものであるテストを作るしようとしている:
describe "current_has_class_link" do
let(:link_path){ listings_path }
let(:link_text){ "Listings" }
it "should render a normal link if not on current path" do
html = "<a href=\"#{link_path}\">#{link_text}</a>"
current_has_class_link(link_text, link_path).should == html
end
it "should add a class if on the links path" do
# at this point I need to force current_path to return the same as link_path
html = "<a href=\"#{link_path}\" class=\"selected\">#{link_text}</a>"
current_has_class_link(link_text, link_path).should == html
end
end
今、明らかに私は統合を使用することができますこれをテストしますが、それは私にとって過酷なもののようです。 current_page?
が必要なものを返すようにスタブできる方法はありますか?
私は
ActionView::Helpers::UrlHelper.stub(current_page?({controller: 'listings', action: 'index'})).and_return(link_path)
を実行しようとしましたしかし、それは私が本当に理解していないエラーが得られます。
Failures:
1) ApplicationHelper current_has_class_link should add a class if on the links path
Failure/Error: ActionView::Helpers::UrlHelper.stub(current_page?({controller: 'listings', action: 'index'})).and_return(link_path)
RuntimeError:
You cannot use helpers that need to determine the current page unless your view context provides a Request object in a #request method
# ./spec/helpers/application_helper_spec.rb:38:in `block (3 levels) in <top (required)>'
は別の方法はありますか?