[rspec-users] Spec for validation plugin

rob_twf rob.anderton at thewebfellas.com
Fri Jul 27 07:29:27 EDT 2007



David Chelimsky-2 wrote:
> 
> 
> Here are some interesting thoughts on that:
> 
> http://blog.jayfields.com/2006/12/rails-unit-testing-activerecord.html
> 
> Just apply the same thinking to rspec.
> 
> 

Thanks for the pointer, here's what I've done:

In spec_helper.rb:

class << ActiveRecord::Base  
  def source_file    
    File.expand_path("#{RAILS_ROOT}/app/models/#{self.name.underscore}.rb")
  end
end

(I preferred source_file to standard_path - seemed more descriptive)

An then my specification looks like this:

  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.

I suppose the downside is it allows implementation to creep into the specs
but if it saves me having to re-test somebody else's code I can live with
it!

I also looked at 
http://spicycode.com/2007/4/2/rspec-expecation-matchers-part-ii SpicyCode's
RSpec extensions  - the approach here appears to be similar to the code in
my original example as it tests validation with a single invalid example and
if that returns the expected result then assumes the validation will work
for all invalid examples (I may be doing it a disservice here as I've only
quickly looked at the source code). This still leaks implementation into the
specs and I think I prefer the Jay Fields approach as it, in my opinion,
more clearly specifies the expected behaviour of the model.

Thanks again,
Rob





-- 
View this message in context: http://www.nabble.com/Spec-for-validation-plugin-tf4152378.html#a11827396
Sent from the rspec-users mailing list archive at Nabble.com.



More information about the rspec-users mailing list