为public_activity rspec创建工厂

时间:2013-07-25 02:45:32

标签: ruby-on-rails ruby ruby-on-rails-3 rspec factory-bot

我正在使用public_activity gem,我有一个方法可以在删除可跟踪的父级时删除活动记录。如下所示。

def destroy
    @comment = Comment.find(params[:id])
    if @comment.destroy
      @activity = PublicActivity::Activity.where(trackable_type: "Comment", trackable_id: @comment.id).first
        @activity.destroy
        respond_to do |format|
          format.js
        end
      end
  end

我已经尝试了很多不同的名称用于public_activity工厂,但似乎没有工作并返回trait not registered

如何使用正确的名称设置public_activity工厂?

factories.rb

factory :activity do
   trackable_id :answer
   recipient_id :user
   owner_id :user
end

2 个答案:

答案 0 :(得分:3)

盲目回答,试试这个:

factory :activity, class: 'PublicActivity::Activity' do
   trackable_id :answer
   recipient_id :user
   owner_id :user
end

答案 1 :(得分:0)

这适用于FactoryBot:

FactoryBot.define do
  factory :activity, class: 'PublicActivity::Activity' do
    association :trackable, factory: :answer
    association :recipient, factory: :user
    association :owner, factory: :user
  end
end