Testing Models And Fixtures


#1

I have finally decided to look into testing and I am going through
the examples in the AWDWR book and I tried the following and it does
not work… even though I don’t see why it shouldn’t.

===========
posts.yml

basic_post:
id: 1
title: Title
body: Body
created_at: 2006-02-01 00:00:00
updated_at: 2006_02-02 00:00:00

===========
post_test.rb

require File.dirname(FILE) + ‘/…/test_helper’

class PostTest < Test::Unit::TestCase
fixtures :posts

def setup
@post = Post.find(1)
end

def test_create
assert_kind_of Post, @basic_post
assert_equal @basic_post.id, @post.id
assert_equal @basic_post.title, @post.title
assert_equal @basic_post.body, @post.body
assert_equal @basic_post.created_at,
@post.created_at_before_type_cast
assert_equal @basic_post.updated_at,
@post.updated_at_before_type_cast
end

def test_update_title
assert_equal “Title”, @post.title
@post.title = “New Title”
assert @post.save, @post.errors.full_messages.join("; ")
@post.reload
assert_equal “New Title”, @post.title
end

def test_destroy
@post.destroy
assert_raise(ActiveRecord::RecordNotFound) { Post.find(@post.id) }
end

end

===========
Error Message


test_create(PostTest) [test/unit/post_test.rb:11]:

expected to be kind_of?
but was
.

===========

The basic_post record from the fixture is not being stored according
to my error messages in @basic_post nor is it being stored in @posts
[‘basic_post’]. What is up with that Jerry?

Your input would be greatly appreciated

Your Friend,

John K.


#2

You have to set

self.use_instantiated_fixtures = true

in test_helper.rb for this to work (I guess the default setting changed
after the book was published).

Michael

John K. wrote:

I have finally decided to look into testing and I am going through the
examples in the AWDWR book and I tried the following and it does not
work… even though I don’t see why it shouldn’t.


#3

John K. wrote:

title: Title
body: Body
created_at: 2006-02-01 00:00:00
updated_at: 2006_02-02 00:00:00
^ could be a problem

regards

Justin