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

David Chelimsky dchelimsky at gmail.com
Fri Feb 16 18:32:57 EST 2007


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
> >
>


More information about the rspec-devel mailing list