[rspec-users] Checking for Range

David Chelimsky dchelimsky at gmail.com
Mon May 28 09:27:33 EDT 2007


On 5/27/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> I'm testing the validation
> validates_length_of :password, :within => 5..40
>
> In this case I should be receiving a failure but I'm not :(
>
>   it "should require a password within the range of 5..40 characters" do
>     person = create(:password => 'a')
>     person.password.should_not be_nil
>     person.should validate_length_of(:password, 5..40)
>   end
>
> I'm an amature tester, would you tell me how I should verify?
> I haven't fully grasped setting testing goals

The behaviour you're trying to describe is that the User should
require that passwords bear certain qualities - in this case a length
between 5 and 40. So you need to ask yourself how the User should
behave when this requirement is violated, and then specify that it
behaves that way.

Here's how I might handle this:

describe User do
  it "should reject a password of length 4" do
    User.create(:password => "1234").should have(1).error_on(:password)
  end
  it "should accept a password of length 5" do
    User.create(:password => "12345").should have(0).errors_on(:password)
  end
  it "should accept a password of length 40" do
    User.create(
      :password => "1234567890123456789012345678901234567890"
    ).should have(0).errors_on(:password)
  end
  it "should reject a password of length 41" do
    User.create(
      :password => "12345678901234567890123456789012345678901"
    ).should have(1).error_on(:password)
  end
end

And possibly add something about the specific error message in one of the cases.

That make sense?

>
> On 5/27/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> >
> > On 5/27/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> > >   it "should require a password within the range of 5..40 characters" do
> > >     person = create(:last_name => "ruby_tuesday")
> > >     person.password.should_not be_nil
> > >     (5..40) === person.password.length.should
> > >   end
> > >
> > > Its true, it really works, tell all your friends!
> >
> > How does that verify that a person will reject a password length of 4 or
> 41?
> >
> > >
> > >
> > > On 5/27/07, Sam Aaron <sam.maillists at googlemail.com > wrote:
> > > >
> > > > On 27 May 2007, at 8.31 pm, Andrew WC Brown wrote:
> > > > >
> > > > > I thought this would work:
> > > > > person.password.length.should == (5..40)
> > > > >
> > > >
> > > > I think that in this case, you want to use the === operator when
> > > > comparing a number with a range. Instead of trying to see if both of
> > > > the objects (the number and the range) are the same, the === operator
> > > > checks to see if the number is within the range.
> > > >
> > > > Also, make sure you surround your range with parenthesis (which you
> > > > are already doing), and put the range on the left hand side of the
> > > > === operator for it to work. Have a look at this quick irb session
> > > > for an example of what I mean:
> > > >
> > > > >> (10..50) == 15
> > > > => false
> > > > >> (10..50) === 15
> > > > => true
> > > > >> 10..50 === 15
> > > > ArgumentError: bad value for range
> > > >          from (irb):6
> > > > >> 15 === 10..50
> > > > ArgumentError: bad value for range
> > > >          from (irb):7
> > > > >> 15 === (10..50)
> > > > => false
> > > > >>
> > > >
> > > > Hope this helps,
> > > >
> > > > Sam
> > > >
> > > > ---
> > > > http://sam.aaron.name
> > > >
> > > >
> > > > _______________________________________________
> > > > rspec-users mailing list
> > > > rspec-users at rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > >
> > >
> > >
> > > _______________________________________________
> > > rspec-users mailing list
> > > rspec-users at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/rspec-users
> > >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list