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
Kyle Hargraves
2008-May-21 00:23 UTC
[rspec-users] How to write a test for validates_uniqueness_of
On Tue, May 20, 2008 at 6:59 PM, T K <tek.katu at gmail.com> wrote:> 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?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
David Chelimsky
2008-May-21 00:25 UTC
[rspec-users] How to write a test for validates_uniqueness_of
On May 20, 2008, at 6:59 PM, T K wrote:> 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?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> > > -T > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
MaurĂcio Linhares
2008-May-21 00:29 UTC
[rspec-users] How to write a test for validates_uniqueness_of
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 at gmail.com> wrote:> 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 > _______________________________________________ > rspec-users mailing list > rspec-users at 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
Ben Mabey
2008-May-21 00:40 UTC
[rspec-users] How to write a test for validates_uniqueness_of
Try this: http://github.com/joshknowles/rspec-on-rails-matchers/tree/master Maur?cio Linhares wrote:> 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 at gmail.com> wrote: > >> 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 >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> > > > >
Josh Knowles
2008-May-21 03:32 UTC
[rspec-users] How to write a test for validates_uniqueness_of
On 5/20/08, Maur?cio Linhares <mauricio.linhares at 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 at gmail.com web: http://joshknowles.com