Given the following Rails 5 helper method, that relies on the Devise helper signed_in?
:
module ApplicationHelper
def sign_in_or_out_link
if signed_in?
link_to 'Sign Out', destroy_user_session_path, method: :delete
else
link_to 'Sign In', new_user_session_path
end
end
end
And the desire to test that the helper returns a particular link based on the signed-in status of the user:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test 'returns Sign Out when signed in' do
assert_match 'Sign Out', sign_in_or_out_link
end
test 'returns Sign In when not signed in' do
assert_match 'Sign In', sign_in_or_out_link
end
end
The test runner raises an error:
NoMethodError: undefined method 'signed_in?' for #<ApplicationHelperTest:...>
.
While we can stub the method within the test:
class ApplicationHelperTest < ActionView::TestCase
def signed_in?
true
end
# ..
How do I re-stub the signed_in?
method such that it returns false
for the second test?