Hi, I have a model and I am trying to do the testing. So my test code is something like the following def test_first assert_nil Person.find(1) end in this I am expecting the test to run against the development database as only development database has the table. But when I ran, it is failing because the test table is not found. Why is model testing is pointing to test database anyway? Thanks.
on 2007-06-13 06:25
on 2007-06-13 06:39
On 6/12/07, DBC User <email@example.com> wrote: > in this I am expecting the test to run against the development > database as only development database has the table. But when I ran, > it is failing because the test table is not found. Why is model > testing is pointing to test database anyway? > > Thanks. > > > > > Why are you expecting it to run against the development database? It will run against whatever database is specified for the test environment in database.yml. Take a look at http://glu.ttono.us/articles/2006/05/22/guide-envi... to understand Rails environments. Pat
on 2007-06-13 17:01
On 6/13/07, DBC User <firstname.lastname@example.org> wrote: > > Hi, > > I have a model and I am trying to do the testing. So my test code is > something like the following > > def test_first > assert_nil Person.find(1) > end This particular test isn't going to work. find(id) will raise an exception if the row can't be found. If it can be found, it will return the Person object. It will never return nil. So you have a couple of options: 1. Use "assert_raise": assert_raise ActiveRecord::RecordNotFound do Person.find(1) end 2. Use find_by_id, which *will* return nil if the row isn't found: assert_nil Person.find_by_id(1) > > in this I am expecting the test to run against the development > database as only development database has the table. But when I ran, > it is failing because the test table is not found. Why is model > testing is pointing to test database anyway? > It's because your test file includes "test_helper.rb" (as it should), which sets the RAILS_ENV variable to "test". You're fighting Rails here. If you create an empty test database and then run "rake test", it will copy the development structure to the test database and run the tests over there for you. The reason for a separate test database is that you want a controlled set of data to work with. The data in your development database is constantly changing, so your test results wouldn't be stable.
on 2007-06-14 05:36