Help needed in UNIT Testing

hi guys,

I needed help in Unit test. i just try to save new data intomy test
database.
but save method does not works for me. the result shows here

def test_new_avail
avail = Avail.new(:vendor_id => 1,
:last_mod_by => ‘Murali’,
:updated_at => '2006-08-17 13:14:59)
assert avail.valid?
assert avail.save
end

is not true
for both assert.

it causes failures not errors while testing.

Plz tell me how to save data from my test class. i need to save data
from this method not from my fixture.

advance Thx

regards,
Narayana.

Narayana K. wrote:

hi guys,

I needed help in Unit test. i just try to save new data intomy test
database.
but save method does not works for me. the result shows here

def test_new_avail
avail = Avail.new(:vendor_id => 1,
:last_mod_by => ‘Murali’,
:updated_at => '2006-08-17 13:14:59)
assert avail.valid?
assert avail.save
end

is not true
for both assert.

it causes failures not errors while testing.

Plz tell me how to save data from my test class. i need to save data
from this method not from my fixture.

advance Thx

regards,
Narayana.

If your Avail model has an association with a Vendor model then you
can’t associate them by setting avail.vendor_id = 1. You need to do it
the same way you would in regular Rails code.

def test_new_avail
avail = Avail.new(:last_mod_by => ‘Murali’,
:updated_at => '2006-08-17 13:14:59)

avail.vendor = Vendor.find_by_id(1)

assert avail.valid?
assert avail.save
end

Jonathan T. wrote:

If your Avail model has an association with a Vendor model then you
can’t associate them by setting avail.vendor_id = 1. You need to do it
the same way you would in regular Rails code.

def test_new_avail
avail = Avail.new(:last_mod_by => ‘Murali’,
:updated_at => '2006-08-17 13:14:59)

avail.vendor = Vendor.find_by_id(1)

assert avail.valid?
assert avail.save
end

I forgot to mention, you’ll need to make sure you load your vendor
object into the database using a fixture first. That call to find_by_id
could probably use some asserting as well.

Hi!

assert avail.valid?

one trick I find convenient to get more accurate information in that
case is
to do something like:

assert avail.valid?, “The model is invalid:
#{avail.errors.full_messages}”

hth

Thibaut

Thibaut Barrère wrote:

one trick I find convenient to get more accurate information in that case
is to do something like:

assert avail.valid?, “The model is invalid: #{avail.errors.full_messages}”

Unless you use : assert_valid :slight_smile:
hi,
Thx for Both Guys.
now
assert avail.valid?
assert avail.save
NO Errors & No Failures.

It passes the both the test, now the problem is, it does not save data
into the test database. From fixtures to database, transaction takes
place successfully.
but for save method the transaction does not take place.

Plz help me to solve this problem.

advance Thx

regards,
Narayana.

Narayana K. wrote:

Thx for Both Guys.
Plz help me to solve this problem.

advance Thx

regards,
Narayana.


Posted via http://www.ruby-forum.com/.

A normal test cycle runs like this…

setup

  1. clear the DB
  2. load the data from fixtures into the DB

run tests
3. start a transaction
4. run the test
5. rollback the transaction
6. lather, rinse, repeat (from 3)

So if you save data to the DB from one test, you won’t see any trace of
it in the DB when you are done.

If you so desire, you can load all of your fixtures into your test db
by using
rake db:fixtures:load

_Kevin

Jonathan,

Why can’t you manually set vendor_id?

I’ve done it several times when I wanted to avoid loading the
associated record.

The only problem I can see is where you need to create the record and
immediately use the association…

No problems yet - but I want to make sure I’m not shooting myself in
the foot.

Starr

one trick I find convenient to get more accurate information in that case
is to do something like:

assert avail.valid?, “The model is invalid: #{avail.errors.full_messages}”

Unless you use : assert_valid :slight_smile: