Hi, I have to test one of my models, but it uses a seed database. How do I delete everything on my database and create it again from seed (default code for some examples) for every one of my unit tests? Right now, when I run rake db:test:units (and if I put something on my code like User.all.count) it says that there 2 users at the database (but with nil paramters except id). If I do a setup code to create the new instances at the database (as I want), it creates them, but there are still those 2 trash users there which makes my model not to work. Thanks in advance -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Jan 17, 2012 at 4:04 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Hi, I have to test one of my models, but it uses a seed database. > > How do I delete everything on my database and create it again from seed > (default code for some examples) for every one of my unit tests? > > Right now, when I run rake db:test:units (and if I put something on my > code like User.all.count) it says that there 2 users at the database (but > with nil paramters except id). If I do a setup code to create the new > instances at the database (as I want), it creates them, but there are still > those 2 trash users there which makes my model not to work. > > Thanks in advance >It is probably not the "optimal" way, but a "practical" way might be to do this: WARNING: this will delete all data in development and test database !! For the development database rake db:drop rake db:create rake db:migrate rake db:seed For the test database RAILS_ENV=test rake db:drop RAILS_ENV=test rake db:create RAILS_ENV=test rake db:migrate RAILS_ENV=test rake db:seed guard # Spork for RSpec This works well as a set-up for rspec (and with static seeded data, e.g. list of countries etc. available also for the tests). Maybe not optimal, but works well. Alternatively, if you do not want to restart from scratch, just deleting all users could be done with rails console> User.delete_allRAILS_ENV=test rails console> User.delete_allPeter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 January 2012 15:04, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, I have to test one of my models, but it uses a seed database. > > How do I delete everything on my database and create it again from seed > (default code for some examples) for every one of my unit tests?I think most nowadays think that factories are the best way of testing, using something like factory_girl. I think the free-to-use-online tutorial railstutorial.org uses factories for testing. That is a good tutorial anyway and well worth working through. If you don''t want to use factories you should use fixtures for populating the test database. I think the Rails Guide on testing covers this. Then rails will automatically reset the test database for each test. I would expect any good tutorial on rails to either use factories or fixtures. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you, I manage to do what I wanted with fixture. Going to trying factory too as soon as I can. Thanks for the help On Tue, Jan 17, 2012 at 1:50 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 17 January 2012 15:04, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, I have to test one of my models, but it uses a seed database. > > > > How do I delete everything on my database and create it again from seed > > (default code for some examples) for every one of my unit tests? > > I think most nowadays think that factories are the best way of > testing, using something like factory_girl. I think the > free-to-use-online tutorial railstutorial.org uses factories for > testing. That is a good tutorial anyway and well worth working > through. > > If you don''t want to use factories you should use fixtures for > populating the test database. I think the Rails Guide on testing > covers this. Then rails will automatically reset the test database > for each test. I would expect any good tutorial on rails to either > use factories or fixtures. > > Colin > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.