PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > rspec and inequalities?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
rspec and inequalities?

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 12h35   #1
Mark Thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut rspec and inequalities?

In an old version of rspec, you could do "x.should_be < y", but this
throws a NoMethodError now. What's the proper way to test inequalities
(less than/greater than)?
  Réponse avec citation
Vieux 09/06/2008, 16h26   #2
James H.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rspec and inequalities?

On Jun 9, 7:35am, Mark Thomas <r...@thomaszone.com> wrote:
> In an old version of rspec, you could do "x.should_be < y", but this
> throws a NoMethodError now. What's the proper way to test inequalities
> (less than/greater than)?


It's basically the same:

x.should be < y

James
  Réponse avec citation
Vieux 09/06/2008, 16h52   #3
Mark Thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rspec and inequalities?


> It's basically the same:
>
> x.should be < y
>
> James


Thanks! Is this syntactic sugar for all be_xxx methods? I just noticed
that I can omit the underscore in be_true also. I'm surprised I
haven't noticed that in examples.
  Réponse avec citation
Vieux 11/06/2008, 23h31   #4
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rspec and inequalities?

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jun 9, 2008 at 11:54 AM, Mark Thomas <ruby@thomaszone.com> wrote:

>
> > It's basically the same:
> >
> > x.should be < y
> >
> > James

>
> Thanks! Is this syntactic sugar for all be_xxx methods?



No.

1.should be > 0
will work, but say:

1.should be_false > 0

will fail when RSpec tries to send #:0? to 1. One might say that this is an
rspec bug, but I'd say that 1.should be_false > 0 is nonsensical, so who
cares.


> I just noticed
> that I can omit the underscore in be_true also. I'm surprised I
> haven't noticed that in examples.



This is because

x.should be y

is interpreted as

x.should eql y

so it will succeed if x.eql?(y) returns a truthy value.

Also note that there's a difference between

x.should be_true
or
x.should be true
or
x.should eql true

and

x.should be

The first three will succeed only iff x == true whereas the latter will
succeed iff x is any truthy value (i.e. anything except nil and false),
likewise

x.should_not be

will succeed iff x is a falsy value (i.e. nil or false)

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Vieux 12/06/2008, 15h42   #5
Mark Thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rspec and inequalities?

On Jun 11, 6:31pm, Rick DeNatale <rick.denat...@gmail.com> wrote:
> > Thanks! Is this syntactic sugar for all be_xxx methods?

>
> No.
>
> 1.should be > 0
> will work, but say:
>
> 1.should be_false > 0
>
> will fail when RSpec tries to send #:0? to 1. One might say that this is an
> rspec bug, but I'd say that 1.should be_false > 0 is nonsensical, so who
> cares.


Actually, what I meant is can
item.should be_valid

be replaced with
item.should be valid

but now that I think about it, rspec probably does some method_missing
magic with the be_ prefix and wouldn't know to use #valid? with the
question mark. Maybe the equivalent is

item.should be valid?
  Réponse avec citation
Vieux 12/06/2008, 22h40   #6
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rspec and inequalities?

[Note: parts of this message were removed to make it a legal post.]

On Thu, Jun 12, 2008 at 10:44 AM, Mark Thomas <ruby@thomaszone.com> wrote:

>
> Actually, what I meant is can
> item.should be_valid
>
> be replaced with
> item.should be valid
>
> but now that I think about it, rspec probably does some method_missing
> magic with the be_ prefix and wouldn't know to use #valid? with the
> question mark. Maybe the equivalent is
>
> item.should be valid?
>
>

I don't think that would work. But I think you could use the rather Yoda
like:

item.valid?.should be

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h38.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14683 seconds with 14 queries