我再次出现错误,一切都在localhost中完美运行,但是当我运行"捆绑exec rake test"时,我有一个错误。
错误
$ bundle exec rake test
1) Error:
UsersProfileTest#test_profile_display:
ActionView::Template::Error: undefined method `supports' for nil:NilClass
app/views/shared/_support_form.html.erb:1:in `_app_views_shared__support_form_html_erb__582342844135452036_70190475648520'
app/views/microposts/_micropost.html.erb:21:in `_app_views_microposts__micropost_html_erb___1173022564425897832_70190475500520'
app/views/users/show.html.erb:19:in `_app_views_users_show_html_erb__2940108544736749993_70190457308940'
test/integration/users_profile_test.rb:14:in `block in <class:UsersProfileTest>'
78 runs, 286 assertions, 0 failures, 1 errors, 0 skips
support_form.html.erb
<% if support = current_user.supports.find_by_micropost_id(micropost.id) %>
<% micropost.supports.each do |support| %>
<% if current_user?(support.user) %>
<button class="btn" type="submit">
<%= link_to "Not Support", [support.micropost, support], method: :delete %>
</button>
<% end %>
<% end %>
<% else %>
<%= form_for ([micropost, @support]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :micropost_id %>
<%= f.hidden_field :user_id %>
<button class="btn" type="submit">
Support
</button>
<% end %>
<% end %>
supports_controller.rb
def create
@support = Support.new(micropost_id: params[:micropost_id], user: current_user)
if @support.save
redirect_to request.referrer || root_url
else
redirect_to request.referrer || root_url
end
end
def destroy
@support.destroy
redirect_to request.referrer || root_url
end
supports_controller_test.rb
require 'test_helper'
class SupportsControllerTest < ActionController::TestCase
def setup
@user = users(:michael)
@micropost = microposts(:orange)
@support = @user.supports.build(micropost_id:@micropost.id)
end
end
sessions_helper.rb
def current_user?(user)
user == current_user
end
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
user_profile_test.rb
require 'test_helper'
class UsersProfileTest < ActionDispatch::IntegrationTest
include ApplicationHelper
def setup
@user = users(:michael)
end
test "profile display" do
get user_path(@user)
assert_template 'users/show'
assert_select 'title', full_title(@user.name)
assert_select 'h1', text: @user.name
assert_select 'h1>img.gravatar'
assert_match @user.microposts.count.to_s, response.body
assert_select 'div.pagination'
@user.microposts.paginate(page: 1).each do |micropost|
assert_match micropost.content, response.body
end
end
end
test_helper
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
include ApplicationHelper
# Add more helper methods to be used by all tests here...
def is_logged_in?
!session[:user_id].nil?
end
# Logs in a test user.
def log_in_as(user, options = {})
password = options[:password] || 'password'
remember_me = options[:remember_me] || '1'
if integration_test?
post login_path, session: { email: user.email,
password: password,
remember_me: remember_me }
else
session[:user_id] = user.id
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
end
答案 0 :(得分:0)
您的current_user
方法返回nil,因为您的终端的值为session[:user_id]
和cookies.signed[:user_id]
,因此if condition
都没有通过。尝试else
并为current_user
提供一些价值。或者您也可以使用pry
之类的调试工具来检查current_user
方法中季节和Cookie的值。