[Rspec-users] Testing has_many, :through relationships

Ben Askins ben.askins at gmail.com
Wed Aug 16 04:09:03 EDT 2006

Hi All,

This is my first post on this list.  I'm a developer working on the
Central Coast of NSW, Australia and came across RSpec via Lachie who
demoed RSpec on Rails at a Sydney rails user group meeting a couple of
months ago.  I'm loving the expressiveness of RSpec and can't really
see myself going back to Test::Unit.

I've come across one oddity though.  I have 3 models, two in a typical
many to many relationship, with the other providing the join:

class Book < ActiveRecord::Base
  has_many :readings
  has_many :readers, :through => :readings

class Reader < ActiveRecord::Base
  has_many :readings
  has_many :books, :through => :readings

class Reading < ActiveRecord::Base
  belongs_to :book
  belongs_to :reader

My spec is simply:

context "Ben" do
  fixtures :readers, :books, :readings
  specify "should have read HHGTTG" do
    ben = readers(:Ben)
    ben.books.should_include books(:HHGTTG)

And the spec fails with:

TypeError in 'Ben should have read HHGTTG'
wrong argument type Book (expected Module)

A quick test in the console reveals that:

ben.books.include? Book.find(:first)

returns true, which I'm assuming is the logical expression that the
spec resolves to.

Is this a problem with my spec or a problem with Rspec on Rails?  I've
only been using it for the last couple of hours so I expect I'm the


More information about the Rspec-users mailing list