I am trying to get a previously passing rspec "view spec" to pass after adding Devise's user_signed_in?
method to the view template in question. The template looks something like this:
<% if user_signed_in? %>
Welcome back.
<% else %>
Please sign in.
<% endif %>
The view spec that was passing looks something like this:
require "spec_helper"
describe "home/index.html.erb" do
it "asks you to sign in if you are not signed in" do
render
rendered.should have_content('Please sign in.')
end
end
The error it produces after adding the call to user_signed_in?
is:
1) home/index.html.erb asks you to sign in if you are not signed in
Failure/Error: render
ActionView::Template::Error:
undefined method `authenticate' for nil:NilClass
# ./app/views/home/index.html.erb:1:in `_app_views_home_index_html_erb__1932916999377371771_70268384974540'
# ./spec/views/home/index.html.erb_spec.rb:6:in `block (2 levels) in <top (required)>'
There are plenty of references to this error around the web, but I have yet to find an answer descriptive enough that I can get my test passing again. I believe the problem has something to do with the view (which is being testing in isolation from any models/controllers) not having some key Devise infrastructure available. Your suggestions are appreciated.
Also, once the test passes, how would I test the other path (user already signed in)? I presume it will be very similar. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…