# Includin a statement in each method

**URL:** <https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229>\
**Category:** Rails\
**Created:** [September 10, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229 "2008-09-10T18:21:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/1 "2008-09-10T18:21:48Z")

</div>

I am running tests and I would like to include

logger.info("\n\n STARTING #{get\_method\_name} \n\n")

in the beginning of each method in my account\_test.rb file. How would I  
do that without having to put it in each method??

---

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/2 "2008-09-10T18:25:29Z")

</div>

On 10 Sep 2008, at 17:21, Joel Saltzmanjoelh wrote:

> I am running tests and I would like to include
> 
> logger.info("\n\n STARTING #{get\_method\_name} \n\n")
> 
> in the beginning of each method in my account\_test.rb file. How  
> would I  
> do that without having to put it in each method??

stick it in the setup method.

Fred

---

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/3 "2008-09-10T18:35:58Z")

</div>

Frederick C. wrote:

> stick it in the setup method.
> 
> Fred

Would that just be

setup logger.info("\n\n STARTING #{get\_method\_name} \n\n")

---

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/4 "2008-09-10T20:02:20Z")

</div>

you first need to make sure that logger is defined in test\_helper. If  
you have not done that, you need to add:  
def logger  
RAILS\_DEFAULT\_LOGGER  
end  
in your test\_helper.rb

then in your unit or functional tests, you can add the logger message  
as part of the setup method which runs once before every action in the  
test  
Def setup  
logger.info …  
end  
the setup method is within the class definition

I am assuming that you defined the get\_method\_name in test\_helper as  
well.

On Sep 10, 11:35 am, Joel Saltzmanjoelh \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/5 "2008-09-10T20:05:36Z")

</div>

saljamil wrote:

> you first need to make sure that logger is defined in test\_helper. If  
> you have not done that, you need to add:  
> def logger  
> RAILS\_DEFAULT\_LOGGER  
> end  
> in your test\_helper.rb
> 
> then in your unit or functional tests, you can add the logger message  
> as part of the setup method which runs once before every action in the  
> test  
> Def setup  
> logger.info …  
> end  
> the setup method is within the class definition
> 
> I am assuming that you defined the get\_method\_name in test\_helper as  
> well.
> 
> On Sep 10, 11:35ï¿½am, Joel Saltzmanjoelh \<rails-mailing-l…@andreas-

yeah I did the helper stuff. I didnt realize i could create a setup  
method and that would automatically run before the test methods. Thanks!

---

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/6 "2008-09-10T20:09:27Z")

</div>

def setup  
logger.info("\n\n STARTING #{get\_method\_name} \n\n")  
end

gives me

STARTING setup\_without\_fixtures

in my log. If I put my logger in each method it prints fine.

def test\_account\_create  
logger.info("\n\n STARTING #{get\_method\_name} \n\n")  
assert Account.create(:name =\> “Bob’s Tofu House”, :short\_name =\>  
“bths”)  
end

will print

STARTING test\_account\_create

any ideas why?

---

<div class="post-metadata">

**Author:** ![jsamildanach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jsamildanach](https://www.ruby-forum.com/u/jsamildanach)\
**Post date:** [September 10, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/includin-a-statement-in-each-method/148229/7 "2008-09-10T20:30:32Z")

</div>

I am assuming that the get\_method\_name just gets the name of the  
method that is currently running. In that case, since the logger  
statement in the setup methods, it will get that name which is not  
what you want. I don’t know of any way not to include something in  
each method. You can create an instance variable in each method and  
then pass it to the ‘teardown’ method which runs after each method is  
run (setup runs before each method is run). I am not sure this will  
save you a lot of typing. You can also add to your get\_method\_name the  
logger but you sill have to call that method from each method.

On Sep 10, 1:09 pm, Joel Saltzmanjoelh \<rails-mailing-l…@andreas-
