Ruby Forum RSpec > How to write a test for validates_uniqueness_of

Posted by T K (Guest)
on 21.05.2008 02:00
(Received via mailing list)
Hi,

I have a spec

 it "should have a unique username "

I have a code:

  validates_uniqueness_of :user_name


Now, I don't know how to test this code. In order to test this, do I
need to run `save`?

For example,

@user = User.create(:username => "mike")
@another = User.create(:username => "mike")
@another.save.should be_false

This messes up test database. Is there any better way?

-T
Posted by Kyle Hargraves (Guest)
on 21.05.2008 02:23
(Received via mailing list)
On Tue, May 20, 2008 at 6:59 PM, T K <tek.katu@gmail.com> wrote:
>
> Now, I don't know how to test this code. In order to test this, do I
> need to run `save`?
>
> For example,
>
> @user = User.create(:username => "mike")
> @another = User.create(:username => "mike")
> @another.save.should be_false
>
> This messes up test database. Is there any better way?

I'm not sure what you mean when you say it messes up the test
database, but you can either call :create! for the second creation and
expect an exception:

  lambda { User.create!(:username => 'mike') }.should
raise_error(ActiveRecord::RecordInvalid, /already taken/)

Or you can just call :new and test if it's valid:

  another = User.new(:username => 'mike')
  another.should_not be_valid
  another.should have_at_least(1).errors_on(:username) # or similar

I tend to go with the second option, but see the first used pretty
often as well.

k
Posted by David Chelimsky (Guest)
on 21.05.2008 02:25
(Received via mailing list)
On May 20, 2008, at 6:59 PM, T K wrote:

>
> Now, I don't know how to test this code. In order to test this, do I
> need to run `save`?
>
> For example,
>
> @user = User.create(:username => "mike")
> @another = User.create(:username => "mike")
> @another.save.should be_false
>
> This messes up test database. Is there any better way?

Are you running with transactional_fixtures = true? If not, I'd
strongly recommend that you do. Even if you're not using fixtures,
that setting causes all of your examples to be run in transactions
that get rolled back after each example. That way, your examples can
make db changes, expect the right thing, and then the db is restored
to its previous state.

HTH,
David
Posted by Maurício Linhares (Guest)
on 21.05.2008 02:29
(Received via mailing list)
This plugin does it -> http://code.google.com/p/rspec-on-rails-matchers/

But it's throwing a "forbidden" error right now.

On Tue, May 20, 2008 at 8:59 PM, T K <tek.katu@gmail.com> wrote:
>
>
> -T
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) |
http://codeshooter.wordpress.com/ (en)
João Pessoa, PB, +55 83 8867-7208
Posted by Ben Mabey (mabes)
on 21.05.2008 02:41
(Received via mailing list)
Posted by Josh Knowles (Guest)
on 21.05.2008 05:33
(Received via mailing list)
On 5/20/08, Maurício Linhares <mauricio.linhares@gmail.com> wrote:
> This plugin does it -> http://code.google.com/p/rspec-on-rails-matchers/
>
>  But it's throwing a "forbidden" error right now.

Sorry, moved the plugin over to GitHub
(http://github.com/joshknowles/rspec-on-rails-matchers/tree/master).
Finally finding some time to integrate the many wonderful patches from
folks over the last month or so.

--
Josh Knowles
phone: 509-979-1593
email:  joshknowles@gmail.com
web:    http://joshknowles.com