开发者

How to write unit tests for STI associations Ruby on Rails

开发者 https://www.devze.com 2023-01-17 10:28 出处:网络
What steps should you use when in a need to write unit tests for STI associations. I am completely confused. Please provide 开发者_如何学Csome suggestions or links to some tutorials.

What steps should you use when in a need to write unit tests for STI associations. I am completely confused. Please provide 开发者_如何学Csome suggestions or links to some tutorials. Thanks in advance


Test all 3 classes as you would normally test any individual class:

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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消