[rspec-users] Error when mocking a method in a class

Juanma Cervera lists at ruby-forum.com
Mon Dec 29 06:54:01 EST 2008


Well, it seems tha the error is indeed in the stubbing of the find 
method in the class of the model.

def validate_uniqueness_of(attribute)
  return simple_matcher("model to validate the uniqueness of 
#{attribute}") do |model|
    model.class.stub!(:find).and_return(true)
    !model.valid? && model.errors.invalid?(attribute)
  end
end

And I found that it has something to do with the names of the models or 
attributes.

I have create two models 'Book' and 'Plano'

script/generate rspec_model Book name:string

script/generate rspec_model Plano nombre:string
#in Spanish, although I have not touched the inflector)

===============================

#book.rb
class Book < ActiveRecord::Base
  validates_uniqueness_of :name
end

#book_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Book do
  it do
    Book.new.should validate_uniqueness_of(:name)
  end
end

=============================

#plano.rb
class Plano < ActiveRecord::Base
  validates_uniqueness_of :nombre
end

#plano_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Plano do
  it do
    Plano.new.should validate_uniqueness_of(:nombre)
  end
end

=======================================

The spec in 'Book' pass, but the spec in 'Plano' fails

and I even see in test.log that the there is a select query for Plano 
and none for Book. The method find for class Plano is not being stubbed.

I think there is a bug!!

Juanma



-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list