Test units now loading

Hi

Im getting this error when running a simple unit test from the book im
learning. I have even copy pasted the code and am not sure why this is
not working

the error is

  1. Error:
    test_read_with_fixture_variable(ProductTest):
    RuntimeError: Called id for nil, which would mistakenly be 4 – if you
    really wanted the id of nil, use object_id
    product_test.rb:63:in `test_read_with_fixture_variable’

My YAML file is
version_control_book:
id: 1
title: Pragmatic Version Control
description: How to use version control
image_url: http://…/sk_svn_small.jpg
price: 29.95
date_available: 2005-01-26 00:00:00

My test unit is

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

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

def test_read_with_fixture_variable
assert_kind_of Product, @product
assert_equal @version_control_book.id, @product.id
assert_equal @version_control_book.title, @product.title
assert_equal @version_control_book.description, @product.description
assert_equal @version_control_book.image_url, @product.image_url
assert_equal @version_control_book.price, @product.price
assert_equal @version_control_book.date_available,
@product.date_available
end
end

Ive checked the value of @product.id and it seems its correctly getting
extracted from the database but im not sure why the instance variable
@version_control_book is not getting loaded.

Thanks heaps

@version_control_book is not initialized because the default is no
longer to instantiate all fixtures in that way (there’s a setting in
test_helper that controls that) because it’s a lot slower.
instead use products(: version_control_book)

Fred

You haven’t assigned anything to @version_control_book. I think you need
to do something like this to get it from the fixtures:

@version_control_book = products(:version_control_book)

EXCELLENT! It worked :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs