# Unwanted behavior with "define" method an Test::Unit

**URL:** <https://www.ruby-forum.com/t/unwanted-behavior-with-define-method-an-test-unit/111785>\
**Category:** Ruby\
**Created:** [August 10, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/unwanted-behavior-with-define-method-an-test-unit/111785 "2007-08-10T19:52:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [August 10, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/unwanted-behavior-with-define-method-an-test-unit/111785/1 "2007-08-10T19:52:14Z")

</div>

I’m using “define\_method” to build some unit test helpers.

They look as such:

class Test::Unit::TestCase  
def self.name\_of\_test\_helper  
define\_method(“test\_whatever\_for\_#{custom\_name}”) {  
assert something\_about(custom\_name)  
}  
end  
end

When I run my tests I get pages and pages of this:

./test/model\_helper.rb:7: warning: multiple values for a block  
parameter (0 for 1)  
from ./test/unit/…/test\_helper.rb:54  
./test/model\_helper.rb:7: warning: multiple values for a block  
parameter (0 for 1)  
from ./test/unit/…/test\_helper.rb:53

Everything works out just fine, the tests test what I want them to  
test. I just don’t want all these errors.

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [August 10, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/unwanted-behavior-with-define-method-an-test-unit/111785/2 "2007-08-10T20:08:16Z")

</div>

Hi –

On Sat, 11 Aug 2007, Collin M. wrote:

> end  
> Everything works out just fine, the tests test what I want them to  
> test. I just don’t want all these errors.

What exactly is on the lines mentioned in the error messages?

David

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [August 10, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/unwanted-behavior-with-define-method-an-test-unit/111785/3 "2007-08-10T20:26:18Z")

</div>

line 7: define\_method(“create\_#{model\_name}”) { |attrs|

Line 7 seems to be the problem line.

I think I want to do this:  
define\_method(“create\_#{model\_name}”) { |attrs={}|

But I cannot. I can still use the defined method and not send it an  
argument, but then Ruby complains.

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [August 10, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/unwanted-behavior-with-define-method-an-test-unit/111785/4 "2007-08-10T20:47:20Z")

</div>

Found this:

* * *

Not sure if this is the most elegant way, but:

## define\_method(:method\_name) do |\*args| parameter, parameter2 = \*args parameter2 ||= ‘default’ puts parameter puts parameter2 end

That’ll do for now.
