Hi, I''m writing a specification for a model called Location which has a post_code attribute which should be a valid UK postcode. I''m using the http://svn.designbyfront.com/rails/plugins/validates_as_uk_postcode/ validates_as_uk_postcode plugin to handle the validation but I''m not sure how I should properly specify my model. Heres the model code: class Location < ActiveRecord::Base validates_as_uk_postcode :post_code end In my model spec I want something like this (assume @location has been setup as a new Location instance): it "should be invalid if the post code is incorrectly formatted" do @location.post_code => ''Q1 1QQ'' @location.should_not be_valid @location.should have(1).error_on(:post_code) end But there are many examples of invalid post codes and they are already tested in the plugin''s own test cases. I only really want to test my code, rather than retest the plugin. So my question is how should I write the specification so that it doesn''t duplicate the tests in the plugin but at the same time expresses the correct behaviour of the Location model? Thanks in advance, Rob -- View this message in context: http://www.nabble.com/Spec-for-validation-plugin-tf4152378.html#a11812924 Sent from the rspec-users mailing list archive at Nabble.com.
On 7/26/07, rob_twf <rob.anderton at thewebfellas.com> wrote:> > Hi, > > I''m writing a specification for a model called Location which has a > post_code attribute which should be a valid UK postcode. I''m using the > http://svn.designbyfront.com/rails/plugins/validates_as_uk_postcode/ > validates_as_uk_postcode plugin to handle the validation but I''m not sure > how I should properly specify my model. > > Heres the model code: > > class Location < ActiveRecord::Base > validates_as_uk_postcode :post_code > end > > In my model spec I want something like this (assume @location has been setup > as a new Location instance): > > it "should be invalid if the post code is incorrectly formatted" do > @location.post_code => ''Q1 1QQ'' > @location.should_not be_valid > @location.should have(1).error_on(:post_code) > end > > But there are many examples of invalid post codes and they are already > tested in the plugin''s own test cases. I only really want to test my code, > rather than retest the plugin. > > So my question is how should I write the specification so that it doesn''t > duplicate the tests in the plugin but at the same time expresses the correct > behaviour of the Location model?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 in advance, > Rob > -- > View this message in context: http://www.nabble.com/Spec-for-validation-plugin-tf4152378.html#a11812924 > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
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.
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 end 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) end ( 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.
Seemingly Similar Threads
- Custom Autocomplete Field - Making a field lowercase
- readLines() behaves differently for gzfile connection
- Fwd: rsync 3.0.9 partial file left after CTRL-C WITHOUT using --partial
- readLines() behaves differently for gzfile connection
- validates_format_of on an optional field using :if