Topic: unittesting on models

def test_user_crud
   abc = User.new
   abc.user_name = 'suresh@rknowsys.com'
   abc.password = 'rknowsys'
   abc.first_name = 'suresh'
assert abc.save
end
In rails unit testing  this record is saved  to the database,but the saved record is  not  being displayed into the database.

Re: unittesting on models

1. Check log/test.log. Is the correct SQL being called?

2. The database is (probably) cleared after the test has run.

Enable debugging in your test environment by adding the following to config/environments/test.rb

# Add debugging
require 'ruby-debug'
Debugger.start

Then add a debugger call at the end of your test so that the debugger is started before the test ends. Run the test. When the debugger starts, check your *test* database. Is the record there?

P.S. It's generally advisable to use assert_equal true rather than just assert, so that you know exactly what value is being returned.

Re: unittesting on models

Each test method resets the database. If you want to set something for every test, use fixtures, or do it in the 'def setup; end' part of the unit test.

Re: unittesting on models

steph(an) wrote:

1. Check log/test.log. Is the correct SQL being called?

2. The database is (probably) cleared after the test has run.

Enable debugging in your test environment by adding the following to config/environments/test.rb

# Add debugging
require 'ruby-debug'
Debugger.start

Then add a debugger call at the end of your test so that the debugger is started before the test ends. Run the test. When the debugger starts, check your *test* database. Is the record there?

P.S. It's generally advisable to use assert_equal true rather than just assert, so that you know exactly what value is being returned.

thanks for giving reply.
but i got error like




D:0:Warning: require_gem is obsolete.  Use gem instead.
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- ruby_debug (MissingSourceFile)
    from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
    from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
    from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
    from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
    from D:/rubyworkspace/contact_market/config/../config/environments/test.rb:1:in `load_environment'
    from C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/initializer.rb:204:in `load_environment'
    from C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
    from C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/initializer.rb:201:in `load_environment'
     ... 9 levels...
    from D:\rubyworkspace\contact_market/test/unit/user_test.rb:1
    from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
    from C:/RadRails/plugins/org.rubypeople.rdt.testunit_0.8.0.604272100PRD/ruby/RemoteTestRunner.rb:260



plz solve this & give me replysoon
thanx in advance

Re: unittesting on models

Install the ruby-debug gem.

Or, just add a massive loop to the end of the test so that you can inspect the database before it finishes.

Both suggestions are of course only necessary if you don't trust the Rails testing framework. Since the test passes and data is meant to be removed from the database after each test, I'm sure everything's fine. Have you tried using the model from the console?

Last edited by steph(an) (2007-09-28 07:27:22)