Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 该网站运行良好,但我';“我在运行时出错”;bundle exec rake test“;_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 该网站运行良好,但我';“我在运行时出错”;bundle exec rake test“;

Ruby on rails 该网站运行良好,但我';“我在运行时出错”;bundle exec rake test“;,ruby-on-rails,ruby,Ruby On Rails,Ruby,我已经为我的应用程序添加了一个支持micropost的按钮,如果用户支持micropost,则该按钮将更改为“不支持”。在localhost中,一切都运行得很好,但当我运行“bundle exec rake test”时,出现了一个错误 错误 $ bundle exec rake test 1) Error: UsersProfileTest#test_profile_display: ActionView::Template::Error: undefined method

我已经为我的应用程序添加了一个支持micropost的按钮,如果用户支持micropost,则该按钮将更改为“不支持”。在localhost中,一切都运行得很好,但当我运行“bundle 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

默认情况下,您没有在测试中验证用户,因此
current\u user
返回
nil
。同样的错误也可能发生在生产中,如果用户以前没有登录就访问了该页面

如果您正在使用Desive for auth-请参阅

class SomeControllerTest
我已经在_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 %>
 <%= render 'shared/support_form', micropost: micropost %>
 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
class SomeControllerTest < ActionController::TestCase
  include Devise::TestHelpers

  def setup
    @request.env["devise.mapping"] = Devise.mappings[:user]
    sign_in FactoryGirl.create(:user) # or other method of making/getting test user
  end
end