# Unit tests - NilClass problem

**URL:** <https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133>\
**Category:** Rails\
**Created:** [August 11, 2006, 7:22am UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133 "2006-08-11T07:22:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2006, 7:22am UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133/1 "2006-08-11T07:22:25Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2006, 10:56am UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133/2 "2006-08-11T10:56:20Z")

</div>

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/](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"

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2006, 1:44pm UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133/3 "2006-08-11T13:44:14Z")

</div>

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:in`test\_creation\_of’  
./test/unit/…/test\_helper.rb:32:in `each' ./test/unit/../test_helper.rb:32:in`test\_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 🙂 I’ll try to play with this now and see if I can solve  
it.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 15, 2006, 12:50am UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133/4 "2006-08-15T00:50:56Z")

</div>

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:in`test\_creation\_of’  
> ./test/unit/…/test\_helper.rb:32:in `each' ./test/unit/../test_helper.rb:32:in`test\_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 🙂 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).

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133/5 "2006-08-15T14:18:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/unit-tests-nilclass-problem/102133/6 "2006-08-15T14:16:51Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂 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)
