[rspec-users] have_one and have_present

Luis Lavena luislavena at gmail.com
Fri May 4 23:45:13 EDT 2007

On 5/5/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > yeah, have_present was due "require" been reserved word :-P
> >
> > validate_presence_of was another option, but that will force me to use
> > validate_uniqueness_of, etc...
> >
> > A lot of typing just for one liner of example :-P
> FWIW, you only have to type these things once each time you type them.
> You're going to read them hundreds of times. It's worth the extra
> typing in my view! If the examples are documentation, they should tell
> you, with as little mental mapping as possible, what it is that's
> being described. Using 'validate_presence_of' tells me EXACTLY what
> the code should look like. That's worth a lot to me.

Valid point,

Prior matchers I was using eval to create the specifys...

should_validate_presence_of, uniqueness_of, etc.

I tried to make shorter, but descriptives versions since upgrade.

  # Here start the accessor for examples:
  #  it { @model.should have_present(:attr) } => should not be valid
without :attr

  #  it { @model.should have_unique(:attr) } => should not be valid
with duplicate :attr

  #  it { @model.should have_one(:attr, :through => :assoc) } =>
should has_one :attr through :assoc

  #  it { @model.should belongs_to(:attr, :through => :assoc) } =>
should belongs_to :attr through :assoc
  #  it { @model.should have_many(:attr, :through => :assoc) } =>
should has_many :attr through :assoc

  #  it { @model.should have_and_belongs_to_many(:attr, :through =>
:assoc) } => should has_and_belongs_to_many :attr through :assoc

  # it { @model.should have_no_errors } => should have no errors

Suggestions are welcome ;-)

Luis Lavena
Multimedia systems
Leaders are made, they are not born. They are made by hard effort,
which is the price which all of us must pay to achieve any goal that
is worthwhile.
Vince Lombardi

More information about the rspec-users mailing list