使用设计发出stub_ current_user

时间:2015-02-28 15:27:39

标签: ruby-on-rails ruby ruby-on-rails-4 rspec devise

我正在制作一个使用devise处理我的用户身份验证的rails 4应用程序,这在任何其他控制器或其测试中都没有给我带来任何麻烦。但是当我在我的评论测试中访问current_user中的存根(它是一个多态关联)时,我一直得到:

Failure/Error: allow(comment).to receive(:current_user).with(:user)
#<Comment id: nil, author_id: nil, content: "This is a Comment", commentable_type: "Question", commentable_id: 1, created_at: nil, updated_at: nil> does not implement: current_user

我已经搜索过这个并且没有找到任何帮助,我使用了设计的ControllerHelper方法来控制测试登录,但它没有帮助。我无法弄清楚这个错误的来源,所以即便这样也会非常有帮助。

这是给我带来麻烦的rspec:

session[:user_id] = user.id
allow(comment).to receive(:current_user).with(:user)

以下是评论模型代码:

class Comment < ActiveRecord::Base
  validates :content, presence: true
  validates :author, presence: true

  belongs_to :author, class_name: "User"
  belongs_to :commentable, polymorphic: true
end

1 个答案:

答案 0 :(得分:0)

current_user是Devise中的控制器或辅助方法(查看this代码)但是你试图在模型中模拟它。它没有current_user方法,因此rspec引发异常。

相关问题