Reputation: 1459
I searched about the error and got various solutions for Rspec.
I am using Minitest and could not understand how to implement it in Minitest.
From what I understand, I am supposed to sign-in the user in "test_helper.rb" by calling include Devise::TestHelpers
I am using
Ruby 2.1.1p76
Rails 4.2.5.1
Rake 11.1.2
Minitest
I am getting the following error:
LocationsControllerTest#test_should_get_index:
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
app/views/layouts/_navbar.html.erb:41:in `_app_views_layouts__navbar_html_erb__1573266900144599421_47382981019560'
app/views/layouts/application.html.erb:19:in `_app_views_layouts_application_html_erb___1642478743058061304_47382980397060'
test/controllers/locations_controller_test.rb:9:in `block in <class:LocationsControllerTest>'
_navbar.html.erb:41
<% if current_user %>
test_helpers.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
test/controllers/locations_controller_test.rb
require 'test_helper'
class LocationsControllerTest < ActionController::TestCase
setup do
@location = locations(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:locations)
end
test "should get new" do
get :new
assert_response :success
end
test "should create location" do
assert_difference('Location.count') do
post :create, location: { long_name: @location.long_name, short_name: @location.short_name }
end
assert_redirected_to location_path(assigns(:location))
end
test "should show location" do
get :show, id: @location
assert_response :success
end
test "should get edit" do
get :edit, id: @location
assert_response :success
end
test "should update location" do
patch :update, id: @location, location: { long_name: @location.long_name, short_name: @location.short_name }
assert_redirected_to location_path(assigns(:location))
end
test "should destroy location" do
assert_difference('Location.count', -1) do
delete :destroy, id: @location
end
assert_redirected_to locations_path
end
end
test/fixtures/users.yml
one:
email: [email protected]
encrypted_password: abc
sign_in_count: 2
current_sign_in_at: Time.now
Upvotes: 0
Views: 429
Reputation: 8212
You first need to add a call to the Devise minitest helpers in your test_helper.rb
class ActionController::TestCase
include Devise::Test::ControllerHelpers
end
Or if you have an older version of Devise:
class ActionController::TestCase
include Devise::TestHelpers
end
You then need to add a call to sign_in
before each test where a user should be signed in. In your example, change the setup to:
setup do
sign_in users(:one)
@location = locations(:one)
end
As @Iceman points out, this is documented in the Devise README.
Upvotes: 3