[Rails] rails models, logging and testing

Łukasz Piestrzeniewicz bragi.ragnarson at gmail.com
Wed Mar 1 15:33:32 GMT 2006


On 01/03/06, Surendra Singhi <efuzzyone at netscape.net> wrote:
> Hello,
>   I am using rails 1.0 and I have following questions. Please help me.
>
> [1]  How can I do logging in the rails models? I checked the wiki but didn't find
> anything which I explained how to do logging in models.

Use following statement:

RAILS_DEFAULT_LOGGER.info "This will be logged with 'info' level"

But please not use it in your models. Rather, use it in your tests.
Then even if you forgot to remove logging statements they will not
have impact on performance or log size of your application.

> [2] Say I have a certain model, when I declare a function in the model, and
> try to use that function from a view or a unit test, I get an error. For views
> the error occurs only once during the first execution but for unit tests it is
> repeatable.
> What am I doing wrong? Is it something strange and unexpected?

Doesn't look like. Could you provide more info on error?

> [3] During testing, if I have a fixture file called products.yml and some
> named entries in it, I get an error if I try to use those named entries as
> variable named in my unit tests.
>
> The agile book says:
>
> "Each named fixture is also automatically "found" using
> an Active Record finder method and put in an instance variable named for
> the fixture."
>
> But when I am trying to repeat this I am getting errors, am I missing
> something or doing something wrong or have other people also experience the
> same problem?

From Rails 0.14 and onwards fixtures are no longer put in instance
variables. You should either use:

fixtures :users
users(:bob)

or modify the file test/test_helper.rb and change value of
self.use_instantiated_fixtures to true. Second method will slow down
tests (in case of large fixture sets considerably).

--
Łukasz Piestrzeniewicz
http://ragnarson.blogspot.com


More information about the Rails mailing list