First I wanted to say, wow Rails is so much nicer than the last platform I worked with (some home-grown platform over asp.net). However there are a few things that just seem to bug me a bit. * Unit testing Partials? I can''t find any documentation on a good way to do a unit test on a View - in particular, a Partial. In most cases, my partials are simply things that take some variables (say, @people), and output them as HTML (say, a table with alternating colors). If there''s a good way to give my partial a set of known inputs and scan the HTML it produces I can''t find it documented. Yes, I know that I can have functional tests that test views as well as models; but it feels a bit cumbersome to test database-stuff when all I really want to do is test turning an array into a HTML string. Wouldn''t it be much cleaner to unit test both the models and the views separately; and have the functional test only make sure that the correct methods on the views and models are being performed? Should I be using a helper function for these instead of partials? At least it''s clear how to unit test those. * The idea that "rake" copies stuff from a pre-existing database rather than creating one from scratch. Perhaps the most in need of testing part of our system is how the database gets populated (a bunch of scripts extracting info from proprietary formats), and "rake" seems to avoid that part completely. Is there a good way to make "rake" not copy a database, but run a database creation script instead? I''m hoping they''re must a matter of me not reading the documentation well. Any tips to make these aspects of testing rails applications easier?
On Tuesday 23 August 2005 22:23, Ron M wrote:> Is there a good way to make "rake" not > copy a database, but run a database creation > script instead?Yes. The simplest one is do redefine the task that copies the structure desc "Recreate the test databases from the development structure" task :clone_structure_to_test => :create_test_database and add a few new ones. See below. I''m only using this with PgSQL and haven''t adapted the stuff for the other DBMS. Michael desc "Recreate an empty test database" task :create_test_database => :purge_test_database do create_structure("test") end desc "Empty the test database" task :purge_test_database => [ :environment, :update_test_ddl ] do purge_database("test") end def purge_database(env) abcs = ActiveRecord::Base.configurations case abcs[env]["adapter"] when "postgresql" ENV[''PGHOST''] = abcs[env]["host"] if abcs[env]["host"] ENV[''PGPORT''] = abcs[env]["port"].to_s if abcs[env]["port"] ENV[''PGPASSWORD''] = abcs[env]["password"].to_s if abcs[env] ["password"] ActiveRecord::Base.remove_connection `dropdb -U "#{abcs[env]["username"]}" #{abcs[env]["database"]}` `createdb -T template0 -U "#{abcs[env]["username"]}" #{abcs[env] ["database"]}` else raise "Unknown database adapter ''#{abcs[env]["adapter"]}''" end end def create_structure(env) abcs = ActiveRecord::Base.configurations case abcs[env]["adapter"] when "postgresql" ENV[''PGHOST''] = abcs[env]["host"] if abcs[env]["host"] ENV[''PGPORT''] = abcs[env]["port"].to_s if abcs[env]["port"] ENV[''PGPASSWORD''] = abcs[env]["password"].to_s if abcs[env] ["password"] ActiveRecord::Base.remove_connection `psql -U "#{abcs[env]["username"]}" -f db/create_#{env}_db.sql #{abcs[env]["database"]}` else raise "Unknown database adapter ''#{abcs[env]["adapter"]}''" end end -- Michael Schuerig Those people who smile a lot mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You