1st attempt at rails testing - could really use some help

Howdy, I’m attempting my first rails test & I could use some help… I
have the test posted at http://rafb.net/paste/results/YjWkIh45.html
when I run it, the test fails, but the db table is never restored & it
seems that the fixtures file was never loaded… Any ideas as to what
I’m doing wrong? Thanks!

randy marmer wrote:

Howdy, I’m attempting my first rails test & I could use some help… I
have the test posted at http://rafb.net/paste/results/YjWkIh45.html
when I run it, the test fails, but the db table is never restored & it
seems that the fixtures file was never loaded… Any ideas as to what
I’m doing wrong? Thanks!

Just a guess, but are you using 1.0?

If so, you may not be aware of a change in the default fixture behavior,
which will not create instance variables like @kase automatically for
you.

Instead, you can retrieve your named data by using kases(:kase) instead:

assert_equal 15, kases(:kase).docAnx

Jeff

Jeff C. wrote:

randy marmer wrote:

Howdy, I’m attempting my first rails test & I could use some help… I
have the test posted at http://rafb.net/paste/results/YjWkIh45.html
when I run it, the test fails, but the db table is never restored & it
seems that the fixtures file was never loaded… Any ideas as to what
I’m doing wrong? Thanks!

Just a guess, but are you using 1.0?

If so, you may not be aware of a change in the default fixture behavior,
which will not create instance variables like @kase automatically for
you.

Instead, you can retrieve your named data by using kases(:kase) instead:

assert_equal 15, kases(:kase).docAnx

Jeff
www.softiesonrails.com

Hey Jeff,

Thanks so much for your reply. Your “1.0” guess is correct… so I went
ahead & replaced @kase with kases(:kase)… unfortunately, this did not
seem to correct the problem - the test still failed, the data in the
kases table is still being wiped out & kases.yml is still empty.

Curiously, however, the error message from the two tests is different…

with @kase, I get:

  1. Error:
    test_update(KaseTest):
    NoMethodError: You have a nil object when you didn’t expect it!
    The error occured while evaluating nil.docAnx
    test/unit/kase_test.rb:8:in `test_update’

1 tests, 0 assertions, 0 failures, 1 errors

Although I still have trouble understanding error messages in rails, the
“nil.docAnx” certainly suggests that @kase, as you pointed out, is not
being used properly…

but, with kases(:kase), i get the folowing:

  1. Error:
    test_update(KaseTest):
    NoMethodError: You have a nil object when you didn’t expect it!
    You might have expected an instance of Array.
    The error occured while evaluating nil.find
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/fixtures.rb:456:in
    kases' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/fixtures.rb:452:in kases’
    test/unit/kase_test.rb:8:in `test_update’

1 tests, 0 assertions, 0 failures, 1 errors

Have you had success testing with 1.0?

Thanks again.

Randy Marmer

matthew clark wrote:

Your link to the code isn’t working anymore.

Without seeing it, do you have in your test class - fixtures :kases?

Do you have in kases.yml?

kase:
datapoint: data
datapoint2: data

matt

Hey Matt,

Something is still not right, but I did get a little further… :~)

The code I ran is here: http://rafb.net/paste/results/PzDwAx30.html

Here is the output from the test:

  1. Failure:
    test_update(KaseTest) [test/unit/kase_test.rb:10]:
    Docanx is negative.
    is not true.

1 tests, 2 assertions, 1 failures, 0 errors

No errors, but;

  1. the results don’t really make sense to me…

  2. somehow, since the intention here was an update, my expectation was
    that the test would attempt to update the number in the docAnx column of
    the record for id: 1… but instead the whole row was overwritten…

  3. the kases table was not reloaded, the test data is still there…

kases is a table of many columns & rows… I’m attempting to update a
single value in the column of a single record…

Somehow, even though the column names “id” & “docAnx” are correct,
wondering if the yml file is properly set up…

I checked the agile book again, but I’m afraid I still don’t get it :~(

Randy Marmer

Your link to the code isn’t working anymore.

Without seeing it, do you have in your test class - fixtures :kases?

Do you have in kases.yml?

kase:
datapoint: data
datapoint2: data

matt

well, its failing on line 10, which is your test to see if the object is
initialized properly. That means to me that the yml file is not getting
loaded into the database.

Are you on a Unix machine? Do a mysqldump of the testing database to
see
what it looks like. Or better, log into the db with a mysql client.
You
should see your data in that table. unittests clear the data at the
beginning of a test (unless you do it in tearDown), so you can inspect
the
db’s state after running them.

I am beginning to suspect that there is a problem with your db schema,
or
your model. Show us your schema and model code. I’ll be we’ll find it.

matt

I’m a bit of a newby myself, but its fun to learn. You are wise to
approach
Rails via testing. Its how I’ve done it. You will be a better Rubista
for
it.

Your problem is in your .yml file. Your database schema has a bunch of
NOT
NULL fields. Your yml file does not define those fields, so the object
can
not be inserted into the db when fixtures is doing its thing. That one
bit
me once too.

Try and write a test specifically testing that the yml objects were
loaded
properly. When those go green, your yml is good. The best advice I can
give you is that when your test fails, and it isn’t clear why, it means
you
are taking too large of steps. Break it down, and make sure each little
step is green before taking another baby step.

That is a hard lesson to learn. This book will help drive it home for
you.
It is available on Orielly’s Safari, among other places -

Test-Driven Development By ExampleBy Kent
Beckhttp://www.informit.com/author_bio.asp/ISBN=0321146530
matt

matthew clark wrote:

I’m a bit of a newby myself, but its fun to learn. You are wise to
approach
Rails via testing. Its how I’ve done it. You will be a better Rubista
for
it.

Your problem is in your .yml file. Your database schema has a bunch of
NOT
NULL fields. Your yml file does not define those fields, so the object
can
not be inserted into the db when fixtures is doing its thing. That one
bit
me once too.

Try and write a test specifically testing that the yml objects were
loaded
properly. When those go green, your yml is good. The best advice I can
give you is that when your test fails, and it isn’t clear why, it means
you
are taking too large of steps. Break it down, and make sure each little
step is green before taking another baby step.

That is a hard lesson to learn. This book will help drive it home for
you.
It is available on Orielly’s Safari, among other places -

Test-Driven Development By ExampleBy Kent
Beckhttp://www.informit.com/author_bio.asp/ISBN=0321146530
matt

Well Matt,

Thanks hanging in there with me…

I don’t think I’m quite there, but I certainly made progress.

The main things I did are as follows:

  1. changed all the non-nullable fields to nullable - the lazy approach!
  2. did an “alter table kases type=InnoDB;” as per suggestion of Mike
    Clark’s
    Weblog - link in documentation of test_helper.rb

This time, I got through without any errors or failures, but now I’m
kinda confused again. I thought that the point was that your original
data was supposed to be restored to the table… i.e., if I had ten rows
of data in there, then when the test is completed, all ten rows would be
restored…

However, I’m left with only one row & that one contains a “9” for
docAnx, not the original “15”.

Is this correct?!?

Randy

matthew clark wrote:

well, its failing on line 10, which is your test to see if the object is
initialized properly. That means to me that the yml file is not getting
loaded into the database.

Are you on a Unix machine? Do a mysqldump of the testing database to
see
what it looks like. Or better, log into the db with a mysql client.
You
should see your data in that table. unittests clear the data at the
beginning of a test (unless you do it in tearDown), so you can inspect
the
db’s state after running them.

I am beginning to suspect that there is a problem with your db schema,
or
your model. Show us your schema and model code. I’ll be we’ll find it.

matt

Hey Matt,

Thanks again for your willingness to help. I’m quite the nooby at all
of this…

I decided to try testing as a way of going back to the basics, as my
“higher-level” attempt to implement my 1st custom validation has so far
been a complete failure.

As you can see (http://rafb.net/paste/results/KeA9ro38.html), I have a
validate method in the model. My initial intention here was to cause the
valdation to fail so that an error object would be populated so I could
access that to work on message display & learn about how to load &
customize messages, etc.

My initial test (update) was intended to succeed (just update 15 with 9)
& once I had a successful test, to experiment by trying to cause the
test to fail, etc, & then try to use what I would (hopefully) learn by
testing, to iron out what I’m doing wrong with regard to my custom
validation method… WHEW!

Long-story-short, I’m trying to teach myself & without ready
face-to-face access to more knowledgeable folks, these types of painful,
uncertain, circuitous approaches to learning become the only way I can
get from point x to point y…

So, there you have it… I feel like i’m standing here in BVD’s :0

Well, any wisdom you could impart would be much appreciated.

Thanks!!

Randy Marmer wrote:

matthew clark wrote:

I’m a bit of a newby myself, but its fun to learn. You are wise to
approach
Rails via testing. Its how I’ve done it. You will be a better Rubista
for
it.

Your problem is in your .yml file. Your database schema has a bunch of
NOT
NULL fields. Your yml file does not define those fields, so the object
can
not be inserted into the db when fixtures is doing its thing. That one
bit
me once too.

Try and write a test specifically testing that the yml objects were
loaded
properly. When those go green, your yml is good. The best advice I can
give you is that when your test fails, and it isn’t clear why, it means
you
are taking too large of steps. Break it down, and make sure each little
step is green before taking another baby step.

That is a hard lesson to learn. This book will help drive it home for
you.
It is available on Orielly’s Safari, among other places -

Test-Driven Development By ExampleBy Kent
Beckhttp://www.informit.com/author_bio.asp/ISBN=0321146530
matt

Well Matt,

Thanks hanging in there with me…

I don’t think I’m quite there, but I certainly made progress.

The main things I did are as follows:

  1. changed all the non-nullable fields to nullable - the lazy approach!
  2. did an “alter table kases type=InnoDB;” as per suggestion of Mike
    Clark’s
    Weblog - link in documentation of test_helper.rb

This time, I got through without any errors or failures, but now I’m
kinda confused again. I thought that the point was that your original
data was supposed to be restored to the table… i.e., if I had ten rows
of data in there, then when the test is completed, all ten rows would be
restored…

However, I’m left with only one row & that one contains a “9” for
docAnx, not the original “15”.

Is this correct?!?

Randy

Whoops! The list above is incomplete… I also commented out the
validate method in the model… When included, it causes the following:

  1. Failure:
    test_update(KaseTest) [test/unit/kase_test.rb:9]:
    Docanx docAnx is missing or invalid.
    is not true.

I wish I understood what was going on here… :~(

Randy Marmer wrote:
.
.
.
.

Well Matt,

Thanks hanging in there with me…

I don’t think I’m quite there, but I certainly made progress.

The main things I did are as follows:

  1. changed all the non-nullable fields to nullable - the lazy approach!
  2. did an “alter table kases type=InnoDB;” as per suggestion of Mike
    Clark’s
    Weblog - link in documentation of test_helper.rb

This time, I got through without any errors or failures, but now I’m
kinda confused again. I thought that the point was that your original
data was supposed to be restored to the table… i.e., if I had ten rows
of data in there, then when the test is completed, all ten rows would be
restored…

However, I’m left with only one row & that one contains a “9” for
docAnx, not the original “15”.

Is this correct?!?

Randy

Whoops! The list above is incomplete… I also commented out the
validate method in the model… When included, it causes the following:

  1. Failure:
    test_update(KaseTest) [test/unit/kase_test.rb:9]:
    Docanx docAnx is missing or invalid.
    is not true.

I wish I understood what was going on here… :~(

Please excuse that last lame comment - i’m getting a little punchy here.

The reason it failed is because I wrote it to do so & it even gave me
the message it was supposed to! That’s a relief.

But I am still confused as to why the kase table is not being
restored…