0

I am trying to use the application.html.erb file to create a navbar on all of my pages. I want some of the links to only show if a user is logged in, and some to show if a user is not logged in, so I have the following in views/layouts/application.html.erb:

...
<% if user_signed_in? %>
    # Show some links
<% end %>

<% if not user_signed_in? %>
    # Show some other links
<% end %>
...

This works when I go to the site, but the issue comes when I try to test with rake.

When I run my tests, I get the following error:

AppointmentsControllerTest#test_should_get_index: ActionView::Template::Error: Devise could not find the Warden::Proxy instance on your request environment. Make sure that your application is loading Devise and Warden as expected and that the Warden::Manager middleware is present in your middleware stack. If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the Devise::Test::ControllerHelpers module to inject the request.env['warden'] object for you.

All of the similar errors I found online do not seem to apply to this situation. I understand that there is an issue with Devise not being loaded while testing, but cannot figure out how to fix it. I have tried many different things and nothing has worked.

Any help is appreciated. Thanks!

dcod
  • 70
  • 1
  • 9

2 Answers2

1

if your using Devise? try to use current_user.present? not user_signed_in

<% if current_user.present? %>
  <%= some links %>
<%else%>
  <%= some links %>
<% end %> 

and in your ApplicationController add this

    protect_from_forgery with: :exception
    before_action :configure_permitted_parameters, if: :devise_controller?
wiwit
  • 73
  • 1
  • 7
  • 26
0

I think you need to include the Devise test helpers:

class AppointmentsControllerTest < ActionController::TestCase
  include Devise::Test::ControllerHelpers
end

See: https://github.com/plataformatec/devise#controller-tests

Jordan Owens
  • 692
  • 4
  • 10