【发布时间】:2014-02-27 18:04:15
【问题描述】:
我正在使用 public_activity gem 在我的应用程序中生成活动源,在我的模型中,我正在使用 devise 的 current_user 来识别活动的所有者。
class Question < ActiveRecord::Base
...
include PublicActivity::Model
tracked owner: ->(controller, model) { controller.current_user }
...
end
我意识到在模型中引用 current_user 不是常态,但这就是他们recommend doing it 的方式。
这在应用程序中运行良好,但我的 Rspec 测试遇到了问题,出现以下错误:
Failure/Error: expect(create(:question)).to be_valid
NoMethodError:
undefined method `current_user' for nil:NilClass
# ./app/models/question.rb:8:in `block in <class:Question>'
# ./spec/models/question_spec.rb:7:in `block (3 levels) in <top (required)>'
测试本身是典型的:
describe "Factory" do
it "has a valid factory" do
expect(create(:question)).to be_valid
end
end
这是我的工厂:
FactoryGirl.define do
factory :question do
title { Faker::Lorem.characters(30) }
body { Faker::Lorem.characters(150) }
user_id { 1 }
tag_list { "test, respec" }
end
end
如何让我的模型中的这个 current_user 方法在我的测试中工作?
【问题讨论】:
标签: ruby-on-rails rspec devise public-activity