Forum: Ruby on Rails Testing model: test becomes dependent on previous test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Malamute J. (Guest)
on 2006-05-05 23:26
Im having trouble with Testing models. im following the tutorials in the
book Agile Web D. with Rails. if i execute the code
product_test.rb, i get this error:

============================================================
  1) Failure:
test_update2(ProductTest) [test/unit/product_test.rb:30]:
<29.95> expected but was
<99.95>.

3 tests, 11 assertions, 1 failures, 0 errors
============================================================

and heres the product_test.rb code:

============================================================
require File.dirname(__FILE__) + '/../test_helper'

class ProductTest < Test::Unit::TestCase
  fixtures :products

  def setup
    @product = Product.find(1)
  end

  # Replace this with your real tests.
  def test_create
    assert_kind_of Product, @product
    assert_equal 1, @product.id
    assert_equal "Pragmatic Programmer", @product.title
    assert_equal "Best book for the programmer", @product.description
    assert_equal "http://localhost:3000/images/pp.jpg",
@product.image_url
    assert_equal 29.95, @product.price
    assert_equal "2006-05-01 00:00:00",
@product.date_available_before_type_cast
  end

  def test_update
    assert_equal 29.95, @product.price
    @product.price = 99.95
    assert @product.save, @product.errors.full_messages.join("; ")
    @product.reload
    assert_equal 99.95, @product.price
  end

  def test_update2
    assert_equal 29.95, @product.price
    @product.price = 59.95
    assert @product.save, @product.errors.full_messages.join("; ")
    @product.reload
    assert_equal 59.95, @product.price
  end
end
============================================================

it reports an error (<29.95> expected but was <99.95>)) in test_update2.
it seems test_update2 becomes dependent on the previous test, which it
should not. if i add new test methods (such as test_destroy,
test_validate,...), they also become dependent on previous tests. but
the book says "...the database is put back to its default state before
the next test method is run."

so how do i correct this?
Malamute J. (Guest)
on 2006-05-05 23:29
btw, im using ubuntu dapper beta. i installed rails 1.0 and ruby 1.8.4
using synaptic manager.
Derrick S. (Guest)
on 2006-05-05 23:37
(Received via mailing list)
> so how do i correct this?
>

If tests become dependent on each other, it means the our database
transactions are not working correctly.  Does your database support
transactions?  Are transactional fixtures enabled?  Can't remember
off the top of my head where to check for this...probably environment/
test.rb

Look in your test log.  Are your tests wrapped with "START
TRANSACTION" and "COMMIT" calls?

-Derrick S.
Malamute J. (Guest)
on 2006-05-05 23:52
Derrick S. wrote:

>
> If tests become dependent on each other, it means the our database
> transactions are not working correctly.  Does your database support
> transactions?  Are transactional fixtures enabled?  Can't remember
> off the top of my head where to check for this...probably environment/
> test.rb
>
> Look in your test log.  Are your tests wrapped with "START
> TRANSACTION" and "COMMIT" calls?
>
> -Derrick S.

how do i know if my db supports transactions? im using mysql.

the test_helper.rb, by default, has this:

self.use_transactional_fixtures = true
self.use_instantiated_fixtures  = false


i tried setting self.use_instantiated_fixtures to true, still the same
error.

please bear with me cause im still learning and following the tutorials
on the book, code word by word. thanks.
Alex W. (Guest)
on 2006-05-06 01:08
malamute jute wrote:
> Derrick S. wrote:
>
>>
>> If tests become dependent on each other, it means the our database
>> transactions are not working correctly.  Does your database support
>> transactions?  Are transactional fixtures enabled?  Can't remember
>> off the top of my head where to check for this...probably environment/
>> test.rb
>>
>> Look in your test log.  Are your tests wrapped with "START
>> TRANSACTION" and "COMMIT" calls?
>>
>> -Derrick S.
>
> how do i know if my db supports transactions? im using mysql.
>
> the test_helper.rb, by default, has this:
>
> self.use_transactional_fixtures = true
> self.use_instantiated_fixtures  = false
>
>
> i tried setting self.use_instantiated_fixtures to true, still the same
> error.
>
> please bear with me cause im still learning and following the tutorials
> on the book, code word by word. thanks.

Try setting self.use_transactional_fixtures = false

That will prevent the tests from using transactions meaning that the
fixtures will be purged and reloaded the old fashioned way after each
test method.  It's slower but it works.

If it works after you do this, then your database does not support
transactions.  I recommend using the latest version of MySQL and be sure
the table type you are using supports transactions.
Malamute J. (Guest)
on 2006-05-07 03:08
Alex W. wrote:
>
> Try setting self.use_transactional_fixtures = false
>
> That will prevent the tests from using transactions meaning that the
> fixtures will be purged and reloaded the old fashioned way after each
> test method.  It's slower but it works.
>
> If it works after you do this, then your database does not support
> transactions.  I recommend using the latest version of MySQL and be sure
> the table type you are using supports transactions.

It worked! my test_helper.rb now's this:

self.use_transactional_fixtures = false
self.use_instantiated_fixtures  = false

my other test methods such as test_destroy and test_validate work
successfully. That means my database doesnt support transactions? But im
using mysql 5.0.19-3 already (installed using synaptic in ubuntu). Or
probably my table type is the problem? Will check. Anyways, im glad it
is working now. Many thanks.
Ray B. (Guest)
on 2006-05-07 03:43
(Received via mailing list)
malamute jute wrote:

> That means my database doesnt support transactions? But im
> using mysql 5.0.19-3 already (installed using synaptic in ubuntu). Or
> probably my table type is the problem? Will check.

Yes, you have MyASM tables which don't support transactions.
If you want transactional support, you need to swithch to InnoDB style
tables.


--

Ray
Malamute J. (Guest)
on 2006-05-07 03:46
alex/ray, you guys were right. im using phpmyadmin and it defaults to
myisam type. i recreated my database/tables to use InnoDB and switched
back self.use_transactional_fixtures to TRUE, and now its working
properly.

Many thanks for all your help.
This topic is locked and can not be replied to.