Rails Noob Question

Apparently the first time I posted this, it was tacked onto the end of
another thread. Please pardon my repost. Won’t happen again.

Howdy. I’m just getting into Rails, and really enjoying it! I’m coming
from the .NET world, so I’ll embarrassedly admit that I’m running it on
winXP and even using SQLServer (for now, may switch to mysql soon).
Hopefully someday I’ll feel competent enough on my mac to work there,
but
one new thing at a time!

Anyway, I am trying to be a good programmer and use TDD, but am having a
problem with a simple test, mostly copied from the examples on:
http://ar.rubyonrails.org/classes/Fixtures.html

I generated my model classes and even got the scaffold working (ooh,
bad!
not TDD!!). I was starting out messing with unit tests and fixtures,
and
was taking baby steps. Ran into a hitch when it was saying that my
fixture
variable was nil:

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

class MessageTest < Test::Unit::TestCase
fixtures :messages, :tags, :messages_tags

def setup
@message = Message.find(1)
end

def test_create
assert_kind_of Message, @message
assert_not_nil @messages

assert_equal @messages[“peace_message”].title, @message.title

end
end

The three fixtures exist (messages, tags, messages_tags) and have data.
Brute force tests (assert_equal “my message title”, @message.title)
pass.
But the @messages[“peace_message”][“title”] returns an error. I added
the
assert_not_nil call and indeed, that fails. It seems like it should
automatically exist? What am I doing wrong?

Thanks for any help!

Brian

Hi, first of all you don’t do anything wrong.

The problem is that something changed with Rails 1.0.
Before, all the fixtures defined were loaded into your memory and one
can imagine that tests were quite slow if too many fixtures were used.
That’s why they changed it and from now on you’ll have to load your
fixtures instances for yourself.

So in setup you have to do something like:

def setup
@message = Message.find(1)
@message2 = messages(:message_label)
end

I hope that helps…

Markus

Brian D. wrote:

Apparently the first time I posted this, it was tacked onto the end of
another thread. Please pardon my repost. Won’t happen again.

Howdy. I’m just getting into Rails, and really enjoying it! I’m coming
from the .NET world, so I’ll embarrassedly admit that I’m running it on
winXP and even using SQLServer (for now, may switch to mysql soon).
Hopefully someday I’ll feel competent enough on my mac to work there,
but
one new thing at a time!

Anyway, I am trying to be a good programmer and use TDD, but am having a
problem with a simple test, mostly copied from the examples on:
http://ar.rubyonrails.org/classes/Fixtures.html

I generated my model classes and even got the scaffold working (ooh,
bad!
not TDD!!). I was starting out messing with unit tests and fixtures,
and
was taking baby steps. Ran into a hitch when it was saying that my
fixture
variable was nil:

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

class MessageTest < Test::Unit::TestCase
fixtures :messages, :tags, :messages_tags

def setup
@message = Message.find(1)
end

def test_create
assert_kind_of Message, @message
assert_not_nil @messages

assert_equal @messages[“peace_message”].title, @message.title

end
end

The three fixtures exist (messages, tags, messages_tags) and have data.
Brute force tests (assert_equal “my message title”, @message.title)
pass.
But the @messages[“peace_message”][“title”] returns an error. I added
the
assert_not_nil call and indeed, that fails. It seems like it should
automatically exist? What am I doing wrong?

Thanks for any help!

Brian

Markus,

Thanks! That explains it. I was wondering if something had changed in
1.0. I should review the release notes.

Could you please explain what you are doing in this line:

@message2 = messages(:message_label)

Is messages referring to the fixture? Where are you loading the
fixture? Thanks again, and I’ll try to find some 1.0 docs in the
meantime.

Scorpion wrote:

Hi, first of all you don’t do anything wrong.

The problem is that something changed with Rails 1.0.
Before, all the fixtures defined were loaded into your memory and one
can imagine that tests were quite slow if too many fixtures were used.
That’s why they changed it and from now on you’ll have to load your
fixtures instances for yourself.

So in setup you have to do something like:

def setup
@message = Message.find(1)
@message2 = messages(:message_label)
end

I hope that helps…

Markus

I got it! Thanks. I found what I needed to know here:
http://manuals.rubyonrails.com/read/chapter/118

Brian D. wrote:

Markus,

Thanks! That explains it. I was wondering if something had changed in
1.0. I should review the release notes.

Could you please explain what you are doing in this line:

@message2 = messages(:message_label)

Is messages referring to the fixture? Where are you loading the
fixture? Thanks again, and I’ll try to find some 1.0 docs in the
meantime.

Scorpion wrote:

Hi, first of all you don’t do anything wrong.

The problem is that something changed with Rails 1.0.
Before, all the fixtures defined were loaded into your memory and one
can imagine that tests were quite slow if too many fixtures were used.
That’s why they changed it and from now on you’ll have to load your
fixtures instances for yourself.

So in setup you have to do something like:

def setup
@message = Message.find(1)
@message2 = messages(:message_label)
end

I hope that helps…

Markus