0

What steps should you use when in a need to write unit tests for STI associations. I am completely confused. Please provide some suggestions or links to some tutorials. Thanks in advance

Rohit
  • 5,631
  • 4
  • 31
  • 59

2 Answers2

1

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
gleenn
  • 26
  • 2
0

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.

Swanand
  • 12,317
  • 7
  • 45
  • 62
  • what if there is some code in the STI child classes i.e. methods that is being executed after the initialization of the class. I think it would be wise to test that code. – Rohit Sep 23 '10 at 09:17
  • Well yes, as I said, if you customized it, then you will need to run the built-in tests. Also, built-in STI modules will ensure the correct instance of class is loaded, depending on `type`, so if you are changing that, then you can write test case along those lines. My point being, there is nothing generic about STI. Post some code if you have any and we can take it from there. – Swanand Sep 23 '10 at 09:55
  • one of the child classes helps in uploading files another child class is used to store multiple choice answers. And there is a column "value" in the STI parent, where the name of the uploaded file is stored and also the choices selected by the user are saved in that column with each choice having its own record. Hope you have a better idea of the situation now. – Rohit Sep 24 '10 at 05:06