Forum: Ruby on Rails Testing Models And Fixtures

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9904f8acaf66a3c1a4b4434676be11cf?d=identicon&s=25 John Kopanas (Guest)
on 2006-03-20 23:37
(Received via mailing list)
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]:
<nil>
expected to be kind_of?
<Post> but was
<NilClass>.

===========

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 Kopanas
1e90246fc67e699c32e59db89d4ff5c3?d=identicon&s=25 Michael Kaiser (Guest)
on 2006-03-21 00:07
(Received via mailing list)
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 Kopanas 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.
 >
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-21 03:43
(Received via mailing list)
John Kopanas 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
This topic is locked and can not be replied to.