Forum: Ruby on Rails Fixtures just Do Not Work for Me

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.
Thiago A. (Guest)
on 2006-03-23 17:15
(Received via mailing list)
Hello,

I have been struggling to get fixtures to work here with no success.
They just Do Not Work For Me (tm).

I have created a model named Headline with the `generate' script and
modified the test/fixtumes/headlines.yml as follows

----------
gita:
  id: 1
  author: raulseixas
  title: gita
  happened_at: 2005-01-01 00:00:00
  description: gita

               This is the CD reissue of the 1974 album and second solo
release
               by Raul Seixas, an important figure of Brazilian rock.
Acid
               critic of establishment, he pardodies several figures of
               Brazilian showbiz in the rocking "Super-HerĂ³is."

# more fixtures here
----------

My database table has columns named id, author, title, happened_at and
description as expected. My headline_test file is as follows

----------
class HeadlineTest < Test::Unit::TestCase
    fixtures :headlines

    def test_fixture
        h = @headlines['gita']

        puts h.title
    end
end
----------

When I call the test with `ruby test/unit/headline_test', I get the
following output:

----------------
  1) Error:
test_fixture(HeadlineTest):
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
    test/unit/headline_test.rb:25:in `test_fixture'
----------------

The call to the `fixtures' methods is supposed to initalize the
@headlines instance variable, isn't it? I can't see where is my
mistake.

Cheers,

Thiago A.
Alex Y. (Guest)
on 2006-03-23 17:24
(Received via mailing list)
Thiago A. wrote:
> The call to the `fixtures' methods is supposed to initalize the
> @headlines instance variable, isn't it? I can't see where is my
> mistake.
The syntax for getting at fixtures changed between the publication of
AWDWR and Rails 1.0 - I expect that's what's bitten you.  Try
headlines('gita') instead of @headlines['gita'].
Thiago A. (Guest)
on 2006-03-23 18:42
(Received via mailing list)
On 3/23/06, Alex Y. <removed_email_address@domain.invalid> wrote:
> The syntax for getting at fixtures changed between the publication of
> AWDWR and Rails 1.0 - I expect that's what's bitten you.  Try
> headlines('gita') instead of @headlines['gita'].

Thanks, Alex. Works perfectly now.

--
Thiago A.
Thiago A. (Guest)
on 2006-03-23 19:07
(Received via mailing list)
On 3/23/06, Alex Y. <removed_email_address@domain.invalid> wrote:
> The syntax for getting at fixtures changed between the publication of
> AWDWR and Rails 1.0 - I expect that's what's bitten you.  Try
> headlines('gita') instead of @headlines['gita'].

Is this outdated too? Is there any other place that I can go for docs?

http://api.rubyonrails.org/classes/Fixtures.html

Regards,

Thiago A.
Alex Y. (Guest)
on 2006-03-23 19:14
(Received via mailing list)
Thiago A. wrote:
> Is this outdated too? Is there any other place that I can go for docs?
>
> http://api.rubyonrails.org/classes/Fixtures.html

It's not actually outdated per se - the defaults just changed.  You can
actually turn the old behaviour back on.  There's an explanation of what
changed (and why) here:

http://clarkware.com/cgi/blosxom/2005/10/24
Thiago A. (Guest)
on 2006-03-23 19:24
(Received via mailing list)
On 3/23/06, Alex Y. <removed_email_address@domain.invalid> wrote:
> It's not actually outdated per se - the defaults just changed.  You can
> actually turn the old behaviour back on.  There's an explanation of what
> changed (and why) here:
>
> http://clarkware.com/cgi/blosxom/2005/10/24

I need to know how many fixtures are defined in the fixture file.
Having read this I went like

------------
class MyControllerTest < Test::Unit::TestCase

    self.use_instantiated_fixtures  = true
    fixtures :headlines

    def test_full_loaded
        get :show_all_headlines
        assert_equal @headlines.size, assigns(:headline).size
    end

end
------------

Would there be a more recommended way of achieving this? Can I get the
number o fixtures loaded without using instantiated fixtures?

Thanks,

Thiago A.
Alex Y. (Guest)
on 2006-03-23 19:29
(Received via mailing list)
Thiago A. wrote:
> Would there be a more recommended way of achieving this? Can I get the
> number o fixtures loaded without using instantiated fixtures?

In this case, I think you could do Headlines.count.
Thiago A. (Guest)
on 2006-03-23 19:38
(Received via mailing list)
On 3/23/06, Alex Y. <removed_email_address@domain.invalid> wrote:
> In this case, I think you could do Headlines.count.

Thanks for this one. I think I had gone into tunnel mode and looking
on the woring place.

Cheers,

Thiago A.
This topic is locked and can not be replied to.