[rspec-users] rspec causing validates_presence_of to validate twice?

David Chelimsky dchelimsky at gmail.com
Thu Oct 18 00:51:21 EDT 2007


On 10/17/07, Steve <vertebrate at gmail.com> wrote:
> On Wed, 17 Oct 2007 21:31:19 -0500, David Chelimsky wrote:
> >
> > This all may be true but I can't help you diagnose the problem without
> > looking at the code. If you'd kindly pastie the spec and model, I'll
> > be glad to look at them. Otherwise I'm just guessing and that's not
> > working out to well so far.
>
> Rather than burden you with it in its uber-long glory, I started trying to
> rebuild the file piecemeal until failure. What I found is that this
> behavior was causing it to fail:
>
> it 'should only allow access to the username, first_name, last_name, and email address fields' do
>   #User.should_receive(:attr_accessible).with(:username, :first_name, :last_name, :email_address)
>   #load "#{RAILS_ROOT}/app/models/user.rb"
> end
>
> You may recall that from a post I made a few days ago. It seems like a
> "duh" moment now. Of course the "load" statement re-evals the class,
> and just adds to the existing version that Ruby knows about. How would you
> get around this though? Can you?

You'd have to make sure that every declaration in the file is stubbed.
Probably the best way would be to do this:

describe User, "declarations" do
  before(:each) do
    User.stub!(:attr_accessible)
    User.stub!(:validates_presence_of)
  end

  it "should assign attrs accessible" do
    User.should_receive(:attr_accessible).with(:username, :first_name,
:last_name, :etc)
    load "#{RAILS_ROOT}/app/models/user.rb"
  end

  it "should validate presence of password" do
    User.should_receive(:attr_accessible).with(:username, :first_name,
:last_name, :etc)
    load "#{RAILS_ROOT}/app/models/user.rb"
  end
end

Kind of hideous, but it should work.


More information about the rspec-users mailing list