[rspec-users] Spec for validation plugin

rob_twf rob.anderton at thewebfellas.com
Fri Jul 27 08:27:24 EDT 2007

rob_twf wrote:
>   it "should validate that the postcode is correctly formatted" do  
> Location.should_receive(:validates_as_uk_postcode).once.with(:post_code)
>     load Location.source_file
>   end
> That works nicely.

Looks like I was a bit eager. Loading the class in this way causes problems
with other validations. For example, I add a name attribute to my class and
validate it's length like this:

class Location < ActiveRecord::Base 
    validates_length_of :name, :in => 1..50
    validates_as_uk_postcode :post_code 

And have this additional specification:

  it "should be invalid if the name is too long" do
    @location.name => 'x' * 51
    @location.should_not be_valid
    @location.should have(1).error_on(:name)
This fails because the length validation occurs twice giving two (identical)
errors on :name. Removing the post code validation specification allows the
name length validation to work correctly. 

So load() is causing the validation to be defined twice - is there any way
to avoid this?
View this message in context: http://www.nabble.com/Spec-for-validation-plugin-tf4152378.html#a11828149
Sent from the rspec-users mailing list archive at Nabble.com.

More information about the rspec-users mailing list