This is probably basic but I'm not seeing it. I'm using rspec & rspec-rails 1.1.11 on Ubuntu, & rails 2.1.0. Here's (the relevant parts of) my class I'm testing:

class Adtag < ActiveRecord::Base

  # snip

  validates_inclusion_of :size, :in => IAB_SIZES
  validates_presence_of :code
  validates_presence_of :client_id
  validates_presence_of :campaign_id

  # snip

end

 

And here's the spec:

module AdtagSpecHelper
  def valid_adtag_attributes
    {
      :category => "shopping",
      :code => "<some adtag='/code'>goes</here>",
      :client_id => 1,
      :campaign_id => 1,
      :size => "300x250"
    }
  end
end

describe Adtag do
  include AdtagSpecHelper

  before(:each) do
    @adtag = Adtag.create( valid_adtag_attributes )
    @adtag.should be_valid
  end

  it "should be invalid without an approved size" do
    @adtag.attributes = valid_adtag_attributes.except(:size)   # ONE
    @adtag.errors.on(:size).should eql("is not included in the list")  # TWO
    @adtag.should have(1).error_on(:size)   # THREE
    @adtag.should_not be_valid
    @adtag.size = "300x250"
    @adtag.should be_valid
  end
end

When I run this, the line with # ONE isn't getting executed. the attributes= method doesn't change the attributes of the @adtag object and I don't know why - there is no update SQL command in the test log. I can get around this by changing that line to this:

@adtag.update_attribute( :size, nil )

...which works but then at the line with # THREE, another error appears:

1)
'Adtag should be invalid without an approved size' FAILED
expected "is not included in the list", got nil (using .eql?)

...and that is weird because the   "validates_inclusion_of :size, :in => IAB_SIZES" in the model should complain, not give nil.

 

All of this adds up to some general strangeness that seems pretty un-rspec-like. Any ideas or help is most welcome.

 

-Jason

 

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to