该网站工作正常但我运行“捆绑exec rake test”时出错

时间:2015-12-06 12:26:15

标签: ruby-on-rails ruby

我已经为我的应用程序添加了微博的支持按钮,如果用户支持微博,按钮将变为“不支持”。在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

_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 %>

_micropost.html.erb

 <%= render 'shared/support_form', micropost: micropost %>

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

1 个答案:

答案 0 :(得分:1)

默认情况下,您在测试中认证用户很热,因此current_user返回nil。如果用户以某种方式访问​​该页面而没有登录,则可能会在生产中发生同样的错误。

如果您使用设计进行身份验证 - 请参阅devise wiki on test helper

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