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

查看:19
本文介绍了使用 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天全站免登陆