Rspecでビューをテストしてみます。私の見解では、Draperによって生成されたデコレータがあります。このデコレータは、decent_exposuregemによって公開されます。
私はそのようなrspecテストを作成します:
require 'spec_helper'
describe "boats/show.html.slim" do
let(:boat_decorate) { BoatDecorator.new(get_boat) }
let(:search) { Search.new }
before do
view.stub(:boat_decorate) { boat_decorate }
view.stub(:search) { search }
render :template => 'boats/show.html.slim'
end
it 'should see titlte' do
rendered.should have_selector(:h1, :content => boat_decorate.title)
end
end
ヘルパーのスタブで、Draperデコレータを生成します。このデコレータには、ヘルパーを呼び出すメソッドがありますlink_to
。
class BoatDecorator < ApplicationDecorator
decorates :boat
def region_link
h.link_to region_name, '#', :title => region_name
end
end
しかし、このテストを開始すると、エラーが発生します:
1) boats/show.html.slim should see titlte
Failure/Error: render :template => 'boats/show.html.slim'
ActionView::Template::Error:
undefined method `link_to' for nil:NilClass
デコレータによるすべてのヘルパー呼び出しのスタブは必要ありません。では、どうすればよいですか?