使用FactoryGirl测试简单的STI [英] Testing simple STI with FactoryGirl

查看:96
本文介绍了使用FactoryGirl测试简单的STI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它是其他一些专门研究行为的类的基础:

I have got a class, that is the base of some other classes that specializes the behavior:

class Task < ActiveRecord::Base
  attr_accessible :type, :name, :command
  validates_presence_of :type, :name, :command

  # some methods I would like to test

end

CounterTask类从Task继承

The class CounterTask inherits from Task

class CounterTask < Task 
end

在我尝试测试基类之前,这一切都很好,因为它必须具有类型.

This all works fine until I am trying to test the base class, since it must have a type.

FactoryGirl.define do
  factory :task do
    sequence(:name) { |n| "name_#{n}" }
    sequence(:command) { |n|  "command_#{n}" }
  end
end

您将如何测试超类的基本功能?

How would you test the basic functionality of the superclass?

推荐答案

您可以声明工厂的定义,如下所示:

You can declare the definitions of factories as follow:

FactoryGirl.define do
  factory :task, class: 'Task' do
    shop
    currency
    value 10
  end

  factory :counter_task, parent: :task, class: 'CounterTask' do
  end
end

现在您可以测试与其自己的工厂隔离的基类,并且对于每个继承的类也是如此.

And now you can test base class isolated from its own factory and the same for each inherited class.

这篇关于使用FactoryGirl测试简单的STI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆