How to write a test for validates_uniqueness_of

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

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

On Tue, May 20, 2008 at 6:59 PM, T K [email protected] 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

Try this:
http://github.com/joshknowles/rspec-on-rails-matchers/tree/master

On 5/20/08, Maurício Linhares [email protected] 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 K.
phone: 509-979-1593
email: [email protected]
web: http://joshknowles.com

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 [email protected] wrote:

-T


rspec-users mailing list
[email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs