Hi, I would like to test a sorting method that is in the user model, it''s a class method called search. What I would like to do is create 2 users and load the test database with just those 2 users, so that I can call User.search("john") and it would return those two users. Not sure how to clear the test database and populate it just with these 2 users for that specific spec. Thanks in advance! Rai
On Nov 20, 2007 6:57 AM, Raimond Garcia <voodoorai2000 at gmail.com> wrote:> Hi, > > I would like to test a sorting method that is in the user model, it''s > a class method called search. > What I would like to do is create 2 users and load the test database > with just those 2 users, so that I can call > User.search("john") and it would return those two users. > > Not sure how to clear the test database and populate it just with > these 2 users for that specific spec.User.delete_all User.create!(:name => "John") User.create!(:name => "George")> > Thanks in advance! > > Rai > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Wow, Thanks Dave, that worked :) I had tried before deleting all users and creating a couple of new ones, was wondering why it didn''t work. The problem was that I was using the create method instead of create!, thus, my users where silently being validated and not saved, as I was missing the password parameters and the such. Thanks again! Keep up the great work with rSpec! Rai On Nov 20, 2007, at 3:40 PM, David Chelimsky wrote:> On Nov 20, 2007 6:57 AM, Raimond Garcia <voodoorai2000 at gmail.com> > wrote: >> Hi, >> >> I would like to test a sorting method that is in the user model, it''s >> a class method called search. >> What I would like to do is create 2 users and load the test database >> with just those 2 users, so that I can call >> User.search("john") and it would return those two users. >> >> Not sure how to clear the test database and populate it just with >> these 2 users for that specific spec. > > User.delete_all > User.create!(:name => "John") > User.create!(:name => "George") > >> >> Thanks in advance! >> >> Rai >> _______________________________________________ >> 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
You may want to create a factory method to create users with valid parameters, overriding any parameters by passing in options. ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a def random_text txt = "" 10.times do txt << ALPHA[rand(52)] end txt # maybe a returning block? can''t remember if that works here end def create_user(options={}) User.create!({ :name => random_text, :password => random_text}.merge(options)) end Nathan Sutton fowlduck at gmail.com rspec edge revision 2910 rspec_on_rails edge revision 2909 rails edge revision 8167 On Nov 20, 2007, at 8:59 AM, Raimond Garcia wrote:> Wow, > > Thanks Dave, that worked :) > I had tried before deleting all users and creating a couple of new > ones, was wondering why it didn''t work. > The problem was that I was using the create method instead of create!, > thus, my users where silently being validated and not saved, as I was > missing the password parameters and the such. > > Thanks again! > Keep up the great work with rSpec! > > Rai > > On Nov 20, 2007, at 3:40 PM, David Chelimsky wrote: > >> On Nov 20, 2007 6:57 AM, Raimond Garcia <voodoorai2000 at gmail.com> >> wrote: >>> Hi, >>> >>> I would like to test a sorting method that is in the user model, >>> it''s >>> a class method called search. >>> What I would like to do is create 2 users and load the test database >>> with just those 2 users, so that I can call >>> User.search("john") and it would return those two users. >>> >>> Not sure how to clear the test database and populate it just with >>> these 2 users for that specific spec. >> >> User.delete_all >> User.create!(:name => "John") >> User.create!(:name => "George") >> >>> >>> Thanks in advance! >>> >>> Rai >>> _______________________________________________ >>> 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
On Nov 20, 2007 10:18 AM, Nathan Sutton <nathan.sutton at gmail.com> wrote:> You may want to create a factory method to create users with valid > parameters, overriding any parameters by passing in options. > > ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a > > def random_text > txt = "" > 10.times do > txt << ALPHA[rand(52)] > end > txt # maybe a returning block? can''t remember if that works here > end > > def create_user(options={}) > User.create!({ :name => random_text, > :password => random_text}.merge(options)) > endThere are also tools that will do this for you: Exemplar, ModelStubbing to name two.> > Nathan Sutton > fowlduck at gmail.com > rspec edge revision 2910 > rspec_on_rails edge revision 2909 > rails edge revision 8167 > > > > > > > On Nov 20, 2007, at 8:59 AM, Raimond Garcia wrote: > > > Wow, > > > > Thanks Dave, that worked :) > > I had tried before deleting all users and creating a couple of new > > ones, was wondering why it didn''t work. > > The problem was that I was using the create method instead of create!, > > thus, my users where silently being validated and not saved, as I was > > missing the password parameters and the such. > > > > Thanks again! > > Keep up the great work with rSpec! > > > > Rai > > > > On Nov 20, 2007, at 3:40 PM, David Chelimsky wrote: > > > >> On Nov 20, 2007 6:57 AM, Raimond Garcia <voodoorai2000 at gmail.com> > >> wrote: > >>> Hi, > >>> > >>> I would like to test a sorting method that is in the user model, > >>> it''s > >>> a class method called search. > >>> What I would like to do is create 2 users and load the test database > >>> with just those 2 users, so that I can call > >>> User.search("john") and it would return those two users. > >>> > >>> Not sure how to clear the test database and populate it just with > >>> these 2 users for that specific spec. > >> > >> User.delete_all > >> User.create!(:name => "John") > >> User.create!(:name => "George") > >> > >>> > >>> Thanks in advance! > >>> > >>> Rai > >>> _______________________________________________ > >>> 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 >
On Nov 20, 2007 10:20 AM, David Chelimsky <dchelimsky at gmail.com> wrote:> On Nov 20, 2007 10:18 AM, Nathan Sutton <nathan.sutton at gmail.com> wrote: > > You may want to create a factory method to create users with valid > > parameters, overriding any parameters by passing in options. > > > > ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a > > > > def random_text > > txt = "" > > 10.times do > > txt << ALPHA[rand(52)] > > end > > txt # maybe a returning block? can''t remember if that works here > > end > > > > def create_user(options={}) > > User.create!({ :name => random_text, > > :password => random_text}.merge(options)) > > end > > There are also tools that will do this for you: Exemplar, > ModelStubbing to name two.Actually - those are geared towards avoiding the DB. What you''d want is Scenarios and FixtureScenarios (and others similar).> > > > > > Nathan Sutton > > fowlduck at gmail.com > > rspec edge revision 2910 > > rspec_on_rails edge revision 2909 > > rails edge revision 8167 > > > > > > > > > > > > > > On Nov 20, 2007, at 8:59 AM, Raimond Garcia wrote: > > > > > Wow, > > > > > > Thanks Dave, that worked :) > > > I had tried before deleting all users and creating a couple of new > > > ones, was wondering why it didn''t work. > > > The problem was that I was using the create method instead of create!, > > > thus, my users where silently being validated and not saved, as I was > > > missing the password parameters and the such. > > > > > > Thanks again! > > > Keep up the great work with rSpec! > > > > > > Rai > > > > > > On Nov 20, 2007, at 3:40 PM, David Chelimsky wrote: > > > > > >> On Nov 20, 2007 6:57 AM, Raimond Garcia <voodoorai2000 at gmail.com> > > >> wrote: > > >>> Hi, > > >>> > > >>> I would like to test a sorting method that is in the user model, > > >>> it''s > > >>> a class method called search. > > >>> What I would like to do is create 2 users and load the test database > > >>> with just those 2 users, so that I can call > > >>> User.search("john") and it would return those two users. > > >>> > > >>> Not sure how to clear the test database and populate it just with > > >>> these 2 users for that specific spec. > > >> > > >> User.delete_all > > >> User.create!(:name => "John") > > >> User.create!(:name => "George") > > >> > > >>> > > >>> Thanks in advance! > > >>> > > >>> Rai > > >>> _______________________________________________ > > >>> 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 > > >
On Nov 20, 2007, at 11:20 AM, David Chelimsky wrote:> On Nov 20, 2007 10:20 AM, David Chelimsky <dchelimsky at gmail.com> > wrote: >> On Nov 20, 2007 10:18 AM, Nathan Sutton <nathan.sutton at gmail.com> >> wrote: >>> You may want to create a factory method to create users with valid >>> parameters, overriding any parameters by passing in options. >>> >>> ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a >>> >>> def random_text >>> txt = "" >>> 10.times do >>> txt << ALPHA[rand(52)] >>> end >>> txt # maybe a returning block? can''t remember if that >>> works here >>> end >>> >>> def create_user(options={}) >>> User.create!({ :name => random_text, >>> :password => random_text}.merge >>> (options)) >>> end >> >> There are also tools that will do this for you: Exemplar, >> ModelStubbing to name two. > > Actually - those are geared towards avoiding the DB. What you''d want > is Scenarios and FixtureScenarios (and others similar). >How about giving FixtureReplacement some love? Scott
Err, Scenarios Nathan Sutton fowlduck at gmail.com rspec edge revision 2910 rspec_on_rails edge revision 2909 rails edge revision 8167 On Nov 20, 2007, at 10:22 AM, Scott Taylor wrote:> > On Nov 20, 2007, at 11:20 AM, David Chelimsky wrote: > >> On Nov 20, 2007 10:20 AM, David Chelimsky <dchelimsky at gmail.com> >> wrote: >>> On Nov 20, 2007 10:18 AM, Nathan Sutton <nathan.sutton at gmail.com> >>> wrote: >>>> You may want to create a factory method to create users with valid >>>> parameters, overriding any parameters by passing in options. >>>> >>>> ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a >>>> >>>> def random_text >>>> txt = "" >>>> 10.times do >>>> txt << ALPHA[rand(52)] >>>> end >>>> txt # maybe a returning block? can''t remember if that >>>> works here >>>> end >>>> >>>> def create_user(options={}) >>>> User.create!({ :name => random_text, >>>> :password => random_text}.merge >>>> (options)) >>>> end >>> >>> There are also tools that will do this for you: Exemplar, >>> ModelStubbing to name two. >> >> Actually - those are geared towards avoiding the DB. What you''d want >> is Scenarios and FixtureScenarios (and others similar). >> > > How about giving FixtureReplacement some love? > > Scott > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Ok, if you want to do it messy and uninformed, do it my way. ;) I''ve heard of Fixture Scenarios but I heard they were broken with edge rspec, but I''ll check out Exemplar, ModelStubbing, and FixtureReplacement. Nathan Sutton fowlduck at gmail.com rspec edge revision 2910 rspec_on_rails edge revision 2909 rails edge revision 8167 On Nov 20, 2007, at 10:22 AM, Scott Taylor wrote:> > On Nov 20, 2007, at 11:20 AM, David Chelimsky wrote: > >> On Nov 20, 2007 10:20 AM, David Chelimsky <dchelimsky at gmail.com> >> wrote: >>> On Nov 20, 2007 10:18 AM, Nathan Sutton <nathan.sutton at gmail.com> >>> wrote: >>>> You may want to create a factory method to create users with valid >>>> parameters, overriding any parameters by passing in options. >>>> >>>> ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a >>>> >>>> def random_text >>>> txt = "" >>>> 10.times do >>>> txt << ALPHA[rand(52)] >>>> end >>>> txt # maybe a returning block? can''t remember if that >>>> works here >>>> end >>>> >>>> def create_user(options={}) >>>> User.create!({ :name => random_text, >>>> :password => random_text}.merge >>>> (options)) >>>> end >>> >>> There are also tools that will do this for you: Exemplar, >>> ModelStubbing to name two. >> >> Actually - those are geared towards avoiding the DB. What you''d want >> is Scenarios and FixtureScenarios (and others similar). >> > > How about giving FixtureReplacement some love? > > Scott > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Hi guys, Thanks for the help, will check out Exemplar and ModelStubbing, for the moment this screencast about FixtureReplacement looks very useful. http://railsnewbie.com/files/fixture_replacement_demo.mov Rai On Nov 20, 2007, at 5:27 PM, Nathan Sutton wrote:> Ok, if you want to do it messy and uninformed, do it my way. ;) > > I''ve heard of Fixture Scenarios but I heard they were broken with edge > rspec, but I''ll check out Exemplar, ModelStubbing, and > FixtureReplacement. > > Nathan Sutton > fowlduck at gmail.com > rspec edge revision 2910 > rspec_on_rails edge revision 2909 > rails edge revision 8167 > > > > > > On Nov 20, 2007, at 10:22 AM, Scott Taylor wrote: > >> >> On Nov 20, 2007, at 11:20 AM, David Chelimsky wrote: >> >>> On Nov 20, 2007 10:20 AM, David Chelimsky <dchelimsky at gmail.com> >>> wrote: >>>> On Nov 20, 2007 10:18 AM, Nathan Sutton <nathan.sutton at gmail.com> >>>> wrote: >>>>> You may want to create a factory method to create users with valid >>>>> parameters, overriding any parameters by passing in options. >>>>> >>>>> ALPHA = (''a''..''z'').to_a + (''A''..''Z'').to_a >>>>> >>>>> def random_text >>>>> txt = "" >>>>> 10.times do >>>>> txt << ALPHA[rand(52)] >>>>> end >>>>> txt # maybe a returning block? can''t remember if that >>>>> works here >>>>> end >>>>> >>>>> def create_user(options={}) >>>>> User.create!({ :name => random_text, >>>>> :password => random_text}.merge >>>>> (options)) >>>>> end >>>> >>>> There are also tools that will do this for you: Exemplar, >>>> ModelStubbing to name two. >>> >>> Actually - those are geared towards avoiding the DB. What you''d want >>> is Scenarios and FixtureScenarios (and others similar). >>> >> >> How about giving FixtureReplacement some love? >> >> Scott >> >> >> >> _______________________________________________ >> 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