[rspec-devel] Meta specifications or spec. helpers available?

Evgeny evgeny.zislis at gmail.com
Sun Feb 18 10:33:58 EST 2007


I am thinking about writing a plugin for rspec_on_rails for all the standard
validate_x helpers in AR.

For example:
   @user.should_validate_presence_of :login
   @user.should_validate_uniqueness_of :email

etc...



On 2/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:
>
> On 2/18/07, Evgeny <evgeny.zislis at gmail.com> wrote:
> > Certainly - I would LOVE to see that blog with this example.
>
>
> http://blog.davidchelimsky.net/articles/2007/02/18/custom-expectation-matchers
>
> > Just be sure to mention that I actually wrote what I wrote based on this
> :
> >
> http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1
>
> Cool. Luke really has a lot of great ideas about RSpec, Rails and
> design in general. I encourage everyone to check out his blog:
> http://www.lukeredpath.co.uk
>
> Cheers,
> David
>
> >
> > Keep up the great work! :)
> >
> >
> > - evgeny
> >
> >
> > On 2/17/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > > On 2/16/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > > > On 2/16/07, Evgeny <evgeny.zislis at gmail.com> wrote:
> > > > > Or perhaps there is a simple way to add my own specifications,
> like:
> > > > >
> > > > >   specify "should be invalid without login" do
> > > > >      @user.should_be_invalid_without :login
> > > > >   end
> > > >
> > > > There will be in a day or two w/ the 0.8-RC1 release. You can do
> this
> > > > now w/ trunk. Here's how I would do it - different names, etc - but
> > > > you can certainly extrapolate your version from this:
> > > >
> > > > module UserSpecHelper
> > > >   def require(attr)
> > > >     return Class.new do
> > > >       def initialize(attr)
> > > >         @attr = attr
> > > >       end
> > > >
> > > >       def matches?(model)
> > > >         @model = model
> > > >         model.send("#{@attr.to_s}=".to_sym, nil)
> > > >         return !model.valid?
> > > >       end
> > > >
> > > >       def failure_message
> > > >         "expected #{@model.inspect} to require #{@attr.inspect}"
> > > >       end
> > > >
> > > >     end
> > > >   end
> > > > end
> > > >
> > > > context "A user (in general)" do
> > > >   include UserSpecHelper
> > > >
> > > >   setup do
> > > >     @user = User.new(:attr1 => value1, :attr2 => value2)
> > > >   end
> > > >
> > > >   specify "should require :attr1" do
> > > >     @user.should require(:attr1)
> > > >   end
> > > > end
> > > >
> > > >   specify "should require :attr2" do
> > > >     @user.should require(:attr2)
> > > >   end
> > > > end
> > > >
> > > > The idea is that you instantiate @user with ALL of its required
> > > > attributes set. The the expectation matcher (the class created in
> > > > #require) sets the one you're interested in to nil.
> > > >
> > > > It would probably perform better if you defined the class
> separately,
> > > > but I was feeling sneaky when I wrote it ;)
> > > >
> > > > You can do this today w/ the trunk, and within a few days w/ 0.8-RC1.
> > > >
> > > > Cheers,
> > > > David
> > >
> > > Actually - this is a GREAT example of a useful custom expectation -
> > > Evegny - do you mind if I blog about it? I'll certainly give you
> > > credit for suggesting it.
> > >
> > > >
> > > > >
> > > > >
> > > > >
> > > > > On 2/16/07, Evgeny <evgeny.zislis at gmail.com> wrote:
> > > > > > Greetings All,
> > > > > >
> > > > > > Is there some way that I could do this :
> > > > > >
> > > > > > module UserSpecHelper
> > > > > >   def required_valid_user_attributes
> > > > > >     { :login => 'monika',
> > > > > >       :email => ' monika at levinski.com',
> > > > > >       :password => 'kinky',
> > > > > >       :password_confirmation => ' kinky',
> > > > > >       :gender => 'f',
> > > > > >       :name => 'Monika Levinski'
> > > > > >     }
> > > > > >   end
> > > > > > end
> > > > > >
> > > > > > context "A user (in general)" do
> > > > > >   include UserSpecHelper
> > > > > >
> > > > > >   setup do
> > > > > >     @user = User.new
> > > > > >   end
> > > > > >
> > > > > >   required_valid_user_attributes.keys.each do
> > |attribute|
> > > > > >     self.class_eval <<-EOT
> > > > > >     specify "should be invalid without #{attribute}" do
> > > > > >       @user.attributes =
> > > > > required_valid_user_attributes.except(#{attribute})
> > > > > >       @user.should_not_be_valid
> > > > > >       @user.errors.on(#{attribute}).should == 'is require'
> > > > > >       @user.#{attribute} =
> > > > > required_valid_user_attributes[attribute.to_sym]
> > > > > >       @user.should_be_valid
> > > > > >     end
> > > > > >     EOT
> > > > > >   end
> > > > > > end
> > > > > >
> > > > > >
> > > > > >
> > > > > > Because typing the same 7 lines 6 or more times does not look
> very
> > DRY to
> > > > > me ...
> > > > > >
> > > > > > Or is there some way to write "specify" helpers or something?
> > > > > >
> > > > > >
> > > > > >
> > > > > > Regards,
> > > > > >
> > > > > > Evgeny
> > > > > >
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > rspec-devel mailing list
> > > > > rspec-devel at rubyforge.org
> > > > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > > > >
> > > >
> > > _______________________________________________
> > > rspec-devel mailing list
> > > rspec-devel at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/rspec-devel
> > >
> >
> >
> > _______________________________________________
> > rspec-devel mailing list
> > rspec-devel at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-devel
> >
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20070218/669008e4/attachment-0001.html 


More information about the rspec-devel mailing list