On 10/16/07, Pat Maddox <[EMAIL PROTECTED]> wrote: > On 10/16/07, Steve <[EMAIL PROTECTED]> wrote: > > I've been thinking about this throughout the day, and is this a pattern > > that should maybe be implemented in a more widespread pattern? For > > example, check that the model makes the requisite calls to the various > > validation functions? It would seem that unless you have some very custom > > validation methods, that you would be testing rails implementation of its > > validators, by running through all of the various checks manually. > > > > I guess maybe it depends on if you view the testing as mostly testing a > > black box, or if you assume some knowledge of the code internals? Thoughts? > > Well, you'll find differing opinions on this.
Yes, you will. This gets very gray. Message expectations (mocks) implicitly know about internals, but they also allow you to spec more accurately WHERE behaviour gets implemented. The argument in favor of loading up the file and expecting an AR declaration (like attr_accessible) is that the behaivour is actually implemented in AR, and it's really not any different from a Message Expectation except it's happening on file load. It also means you don't have to hit the database, which means it runs faster. The argument against is that the declaration is an implementation detail and therefore brittle. > Every time I've done > validations I've written a spec that actually exercises the behavior. > For example > > class Person < ActiveRecord::Base > validates_presence_of :name > end > > describe Person do > it "should not be valid without a name" do > p = Person.new > p.should_not be_valid > p.errors.on(:name).should == "can't be blank" > end > end I've got one project where I do this - it comes from somebody's post on this list (though I'm at a loss for who right now): person.should reject(nil).for(:name).with("can't be blank") person.should reject("").for(:name).with("can't be blank") person.should accept("a name").for(:name) <snip> > An interesting thing is that since I decided it's okay for > attr_accessible, I'm starting to consider whether it may be okay for > other validations. afaik David C doesn't have a firm opinion on this, > and I have to admit that mine is softening up a bit. > > Personally, if I wanted to extract this pattern, I would use a custom > expectation matcher. ... which would probably be implemented like this under the hood: def matches?(target) target.should_receive(:validates_presence_of).with(@expected) load "#{RAILS_ROOT}/app/models/#{target.humanize}.rb" end No? _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users