[rspec-users] Testing Single Table Inheritance

Gerardo Vázquez gerardovr at gmail.com
Sat Mar 24 13:45:49 EDT 2007


Hi all,

I have just started using Rspec in rails and trying to test the
following models:
>>node.rb
class Node < ActiveRecord::Base
   acts_as_tree
end

>>project.rb
class Project < Node
end

>>task.rb
class Task < Project
end

>>milestone.rb
class Milestone < Node
end

Fixtures for table nodes is set global in spec_helper.rb, with the types
set manually in the fixtures file.
one:
  id: 1
  type: Node
  name: Node1
.. etc, 2 fixtures for each class (8 in total)

When i run the default test (check 2 records loaded from fixtures) it
fails. It founds 8 nodes(Failed), 4 project(Failed), 2 task(OK) and 2
milestones(OK). From the rails test server:

[4;35;1mSQL(0.000000)[0m[0mBEGIN[0m
[4;36;1mNode Load(0.000000)[0m[0;1mSELECT * FROM nodes[0m
[4;35;1mNode Columns(0.000000)[0m[0mSHOW FIELDS FROM nodes[0m
[4;36;1mSQL (0.000000)[0m[0;1mROLLBACK[0m
[4;35;1mSQL (0.000000)[0m[0mBEGIN[0m
[4;36;1mProject Columns(0.000000)[0m[0;1mSHOW FIELDS FROM nodes[0m
[4;35;1mProject Load(0.000000)[0m[0mSELECT * FROM nodes WHERE
((nodes.`type` = 'Project' OR nodes.`type` = 'Task'))[0m

It does not specify type for node and adds "Task" type for project.
Running the test with no fixtures set it does not add the "Task" type
for project.

[4;35;1mProject Columns(0.000000)[0m[0mSHOW FIELDS FROM nodes[0m
[4;36;1mProject Load(0.000000)[0m[0;1mSELECT * FROM nodes WHERE
((nodes.`type` = 'Project'))[0m


Here goes the _spec files:

>>node_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
context "Given a generated node_spec.rb with fixtures loaded" do

setup do
    @Nodes = Node.find(:all)
end
specify "fixtures should load 2 Nodes" do
    @Nodes.should have(2).records
end
end

>>project_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
context "Given a generated project_spec.rb with fixtures loaded" do
setup do
    @Projects = Project.find(:all)
end
specify "fixtures should load two Projects" do
    @Projects.should have(2).records
end
end

Should I test all the class in node_spec?
I suppose I`m missing something, any tip?

Thanks in advance

Gerardo


More information about the rspec-users mailing list