mardi 27 novembre 2018

How to test a Rails 5 helper that relies on Devise signed_in? helper, with Minitest?

Given the following Rails 5 helper method, that relies on a the Devise helper signed_in?:

module ApplicationHelper
  def signed_in_or_out
    signed_in? ? "A" : "B"
  end
end

And the desire to test that the helper returns "A" when signed_in? returns true, and to test that the helper returns "B" when signed_in? returns false:

require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase
  test 'returns A when signed in' do
    assert_equal "A", signed_in_or_out
  end

  test 'returns B when not signed in' do
    assert_equal "B", signed_in_or_out
  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?

Aucun commentaire:

Enregistrer un commentaire