Topic: Unable to test model

I m unable to test model which insert value from fixture into database..
I m getting following error is
"@attributes.....e
epected to be kind_off?
<Product> but was
<Array>

ASAP..can any one tell y this error is comming & how to overcome

Re: Unable to test model

Can you post your products.yml fixtures and product_test.rb files? The product.rb file would be good too along with a more detailed error message (stack trace). smile

Railscasts - Free Ruby on Rails Screencasts

Re: Unable to test model

Product.yml file is..

abcd:
  id: 1
  title: xyz
  description: Software Testing Engineer
  image_url: http://.../sk_svn_small.jpg
  price: 29.95
  date_available: 2005-01-26 00:00:00
automation_book:
  id: 2
  title: Pragmatic Project Automation
  description: How to automate your project
  image_url: http://.../sk_auto_small.jpg
  price: 29.95
  date_available: 2004-07-01 00:00:00
future_proof_book:
  id: 3
  title: Future-Proofing Your Tests
  description: How to beat the clock
  image_url: http://.../future.jpg
  price: 29.95
  date_available: 2005-04-27 00:00:00


and

product.rb file contains...

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

class ProductTest < Test::Unit::TestCase
  fixtures :products
  def setup
      @product = Product.find(:all)
  end
  # Replace this with your real tests.
  def test_create
    assert_kind_of Product, @product
    assert_equal 1, @product.id
    assert_equal "xyz", @product.title
    assert_equal "Software Testing Engineer", @product.description
    assert_equal "http://.../sk_svn_small.jpg", @product.image_url
    assert_equal 29.95, @product.price
    assert_equal "2005-01-26 00:00:00",
                      @product.date_available_before_type_cast
  end
end

Last edited by bkchoudhary (2007-01-18 01:02:13)

Re: Unable to test model

Try this in the setup method of ProductTest:

  def setup
      @product = Product.find(:first)
  end

Before you were calling find(:all) which returns an array of many products. With this change it will only return one product which appears to be what you want.

Railscasts - Free Ruby on Rails Screencasts

Re: Unable to test model

I m getting an error which says that ....
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

The method in product_test.rb in which i am getting error is....

  def test_read_with_fixture_variable
    assert_kind_of Product, @product
    @product = Product.find(3)
    assert_equal @binod.id, @product.id
    assert_equal @binod.title, @product.title
    assert_equal @binod.description, @product.description
    assert_equal @binod.image_url, @product.image_url
    assert_equal @binod.price, @product.price
    assert_equal @binod.date_available, @product.date_available
  end

and my product.yml is mention above...

Re: Unable to test model

It doesn't look like the @binod instance variable is set. Where are you setting this?

Railscasts - Free Ruby on Rails Screencasts

Re: Unable to test model

ryanb wrote:

It doesn't look like the @binod instance variable is set. Where are you setting this?

Is that I cant use instance directly..... as shown in the above post. if not then how to set the instance...

Re: Unable to test model

I'm not sure what the @binod instance variable is supposed to contain, so I can't say how to set it. What do you want it to do?

Railscasts - Free Ruby on Rails Screencasts