Unit tests - NilClass problem

Hi!

Yesterday I started to write my unit tests for the fist time using
rails. I’m using this method:
http://blog.codahale.com/2005/12/23/a-rails-howto-simplify-your-unit-tests/

My customer_test.rb looks like this:
require File.dirname(FILE) + ‘/…/test_helper’

class CustomerTest < Test::Unit::TestCase
fixtures :customers

Replace this with your real tests.

def test_create
test_creation_of :model => Customer,
:record => @customer,
:fixture => @first,
:attributes => [:id, :name, :contact_firstname,
:contact_lastname, :contact_address, :contact_country, :contact_state,
:contact_email, :contact_phone, :activationcode]
end
end

and when trying to run it I get:

Loaded suite customer_test
Started
F
Finished in 0.731 seconds.

  1. Failure:
    test_create(CustomerTest)
    [./…/test_helper.rb:31:in test_creation_of' customer_test.rb:13:in test_create’]:

    expected to be kind_of?
    but was
    .

1 tests, 1 assertions, 1 failures, 0 errors"

Any idea what might be wrong here?

Thanks,

John

Reading through the article, but not actually testing the code, I think
the problem is with @customer. You should have a value for @customer.
Maybe you can add @customer = Customer.find(1)

Topher

John M. wrote:

Hi!

Yesterday I started to write my unit tests for the fist time using
rails. I’m using this method:
http://blog.codahale.com/2005/12/23/a-rails-howto-simplify-your-unit-tests/

My customer_test.rb looks like this:
require File.dirname(FILE) + ‘/…/test_helper’

class CustomerTest < Test::Unit::TestCase
fixtures :customers

Replace this with your real tests.

def test_create
test_creation_of :model => Customer,
:record => @customer,
:fixture => @first,
:attributes => [:id, :name, :contact_firstname,
:contact_lastname, :contact_address, :contact_country, :contact_state,
:contact_email, :contact_phone, :activationcode]
end
end

and when trying to run it I get:

Loaded suite customer_test
Started
F
Finished in 0.731 seconds.

  1. Failure:
    test_create(CustomerTest)
    [./…/test_helper.rb:31:in test_creation_of' customer_test.rb:13:in test_create’]:

    expected to be kind_of?
    but was
    .

1 tests, 1 assertions, 1 failures, 0 errors"

topher wrote:

Reading through the article, but not actually testing the code, I think
the problem is with @customer. You should have a value for @customer.
Maybe you can add @customer = Customer.find(1)

Yeah, I bet I need that one. I think we’re progressing here, but now I’m
getting this:

  1. Error:
    test_create(CustomerTest):
    RuntimeError: Called id for nil, which would mistakenly be 4 – if you
    really wa
    nted the id of nil, use object_id
    ./test/unit/…/test_helper.rb:33:in send' ./test/unit/../test_helper.rb:33:intest_creation_of’
    ./test/unit/…/test_helper.rb:32:in each' ./test/unit/../test_helper.rb:32:intest_creation_of’
    test/unit/testcase_test.rb:13:in `test_create’

1 tests, 1 assertions, 0 failures, 1 errors

Thanks already for your help Topher! If you have any more ideas please
keep’em coming :slight_smile: I’ll try to play with this now and see if I can solve
it.

John M. wrote:

topher wrote:

Reading through the article, but not actually testing the code, I think
the problem is with @customer. You should have a value for @customer.
Maybe you can add @customer = Customer.find(1)

Yeah, I bet I need that one. I think we’re progressing here, but now I’m
getting this:

  1. Error:
    test_create(CustomerTest):
    RuntimeError: Called id for nil, which would mistakenly be 4 – if you
    really wa
    nted the id of nil, use object_id
    ./test/unit/…/test_helper.rb:33:in send' ./test/unit/../test_helper.rb:33:intest_creation_of’
    ./test/unit/…/test_helper.rb:32:in each' ./test/unit/../test_helper.rb:32:intest_creation_of’
    test/unit/testcase_test.rb:13:in `test_create’

1 tests, 1 assertions, 0 failures, 1 errors

Thanks already for your help Topher! If you have any more ideas please
keep’em coming :slight_smile: I’ll try to play with this now and see if I can solve
it.

Personally I wouldn’t bother trying to follow the article suggested by
that article - all you are doing in testing ActiveRecord provided CRUD
functionality is duplicate tests already present in ActiveRecord’s own
test suite. Focus on testing your model’s behaviour, constraints and
validations, not on stuff tested elsewhere.

(unit tests shouldn’t even talk to the database, strictly speaking, but
thats an article for another time).

It looks like the article may have been written assuming the use of
instantiated_fixtures. Maybe you could replace @customer with
customers(:customer). Note, I haven’t read the article so could be
well off the mark with this.

Chris

Not having used Coda’s technique, my guess is you haven’t enhanced
your fixtures to have all the necessary attributes. Assuming you
created your Customer model with a generator, you should look in
test/fixtures/customers.yml. The first record has the name “first”
and an id attribute. You’ll need to manually add the remaining
attributes (along with values for each):

:name, :contact_firstname, :contact_lastname, :contact_address,
:contact_country, :contact_state, :contact_email, :contact_phone,
:activationcode

HTH,

Colin

On 8/11/06, John M. [email protected] wrote:

RuntimeError: Called id for nil, which would mistakenly be 4 – if you

Thanks already for your help Topher! If you have any more ideas please
keep’em coming :slight_smile: I’ll try to play with this now and see if I can solve
it.


Colin Strasser
Union Square Internet Development
917.723.6930 (m)
646.219.0332 (f)