4

当我将设计模块添加到我的 Rails 应用程序时,我的测试出现错误

 73) Error:
test_should_update_task(TasksControllerTest):
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
    /home/razor/.gem/ruby/1.9.1/gems/devise-2.1.2/lib/devise/controllers/helpers.rb:56:in `current_user'
    /home/razor/.gem/ruby/1.9.1/gems/devise-2.1.2/lib/devise/controllers/helpers.rb:52:in `user_signed_in?'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/abstract_controller/helpers.rb:53:in `user_signed_in?'
    /home/razor/work/judgement/app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___67557477041607610_29516700'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/template.rb:145:in `block in render'
    /home/razor/.gem/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
    /home/razor/.gem/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/razor/.gem/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/template.rb:143:in `render'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/renderer/template_renderer.rb:59:in `render_with_layout'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/renderer/template_renderer.rb:45:in `render_template'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/renderer/template_renderer.rb:18:in `render'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/renderer/renderer.rb:36:in `render_template'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_view/renderer/renderer.rb:17:in `render'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:110:in `_render_template'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_controller/metal/streaming.rb:225:in `_render_template'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:103:in `render_to_body'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:88:in `render'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_controller/metal/rendering.rb:16:in `render'
    /home/razor/.gem/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    /home/razor/.gem/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/core_ext/benchmark.rb:5:in `block in ms'

93 tests, 36 assertions, 0 failures, 73 errors, 0 skips
Errors running test:functionals! #<RuntimeError: Command failed with status (73): [/usr/bin/ruby19 -I"lib:test" -I"/usr/lib64...]>

application.html.erb 在哪里

<!DOCTYPE html>
<html>
    <head>
        <title>Judgement</title>
        <%= stylesheet_link_tag    "application", :media => "all" %>
        <%= javascript_include_tag "application" %>
        <%= csrf_meta_tags %>
    </head>
    <body>
        <nav>
            <% if user_signed_in? then %>
            <%= link_to "You are #{current_user.email}", edit_user_registration_path %>
            <%= link_to "Exit", destroy_user_session_path, { :method => :delete,
                                                             :confirm => "Are you sure"} %>
            <% else %>
            <%= link_to "Sign In", new_user_session_path %>
            <%= link_to "Register", new_user_registration_path %>
            <% end %>
        </nav>
        <%= yield %>
        <div class="notice">
            <%= notice %>
        </div>
        <div class="alert">
            <%= alert %>
        </div>
    </body>
</html>

所以这看起来像错误,因为“user_signed_in?” 帮手。但我不明白错误的意义何在。

我对所有 Rails 魔法都不是很熟悉。

更新:已解决

我必须添加

class ActionController::TestCase
  include Devise::TestHelpers
end

到我的 test/test_helper.rb

4

2 回答 2

1

我建议使用此功能:https ://gist.github.com/989413 。您的测试将更加孤立。在你的罐子里,你可以像这样存根:helper.stub(:user_signed_in?) {false}。函数 user_signed_in? 应该根据需要返回 true 或 false。

这里有一个类似的问题:Testing helpers in Rails 3 with Rspec 2 and Devise

编辑测试::单元

它认为你应该重新定义一个用 Test::Unit 模拟的方法。像这样的东西:

module Devise
  module Controllers
    module Helpers    
      def user_signed_in?
        true #Return false if you want
      end
    end
  end
end
于 2012-07-23T12:27:10.000 回答
0

如果您将其用于 rspec 中的视图规范,您需要将其存根于view,而不是helper.

allow(view).to receive(:user_signed_in?).and_return(true)

rspec 文档中的来源

于 2016-02-03T16:19:04.553 回答