简体   繁体   English

如何为STI关联编写单元测试Ruby on Rails

[英]How to write unit tests for STI associations Ruby on Rails

What steps should you use when in a need to write unit tests for STI associations. 需要为STI关联编写单元测试时,应使用哪些步骤。 I am completely confused. 我完全感到困惑。 Please provide some suggestions or links to some tutorials. 请提供一些建议或一些教程的链接。 Thanks in advance 提前致谢

Test all 3 classes as you would normally test any individual class: 像通常测试任何一个单独的类一样,测试所有3个类:

class Person < ActiveRecord::Base
  attr_reader :first_name, :last_name
  def initialize
    @first_name = "George"
    @last_name = "Washington"
  end

  def formatted_name
    "#{@first_name} #{@last_name}"
  end
end

class Doctor < Person
  def formatted_name
    "Dr. #{@first_name} #{@last_name}"
  end
end

class Guy < Person
  def formatted_name
    "Mr. #{@first_name} #{@last_name}"
  end
end

describe Person do
  describe "#formatted_name" do
    person = Person.new
    person.formatted_name.should == "George Washington"
  end
end

describe Doctor do
  describe "#formatted_name" do
    doctor = Doctor.new
    doctor.formatted_name.should == "Dr. George Washington"
  end
end

describe Guy do
  describe "#formatted_name" do
    guy = Guy.new
    guy.formatted_name.should == "Mr. George Washington"
  end
end

There is absolutely nothing special in STI relationships that you should write test cases about. 应该编写测试用例的STI关系绝对没有什么特别的。 Since this is a functionality provided by framework, the frameworks comes with a bunch of testcases. 由于这是框架提供的功能,因此框架附带了一堆测试用例。

You only need to write testcases for the functionality you are building. 你只需要为写正在建设的功能测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM