Sinatraで作業すると、ローカルオブジェクトrequest
が作成され、すべてのビューとヘルパーが利用できるようになります。したがって、ApplicationHelper
ヘルパーメソッドを使用してモジュールを作成できます。また、ビューでヘルパーメソッドが呼び出されると、次のrequest
ようにオブジェクトを呼び出すことができます。
module ApplicationHelper
def nav_link_to(text,path)
path == request.path_info ? klass = 'class="current"' : klass = ''
%Q|<a href="#{path}" #{klass}>#{text}</a>|
end
end
今、これをテストしたいのですが、私のテストではrequest
オブジェクトが存在しません。私はそれをあざけることを試みました、しかしそれはうまくいきませんでした。これまでの私のテストは次のとおりです。
require 'minitest_helper'
require 'helpers/application_helper'
describe ApplicationHelper do
before :all do
@helper = Object.new
@helper.extend(ApplicationHelper)
end
describe "nav links" do
before :each do
request = MiniTest::Mock.new
request.expect :path_info, '/'
end
it "should return a link to a path" do
@helper.nav_link_to('test','/test').must_equal '<a href="/test">test</a>'
end
it "should return an anchor link to the current path with class 'current'" do
@helper.nav_link_to('test','/').must_equal '<a href="test" class="current">test</a>'
end
end
end
では、テストで呼び出すことができるように、「ローカル」オブジェクトをどのようにモックすることができますか?