# Assert\_name doesn't exist as a function?

**URL:** <https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986>\
**Category:** Ruby\
**Created:** [April 20, 2011, 3:04am UTC](https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986 "2011-04-20T03:04:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 20, 2011, 3:04am UTC](https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986/1 "2011-04-20T03:04:38Z")

</div>

The script (test.rb):

== START ==  
require ‘rubygems’  
require ‘firewatir’

include Test::Unit  
include Test::Unit::Assertions  
include Test::Unit::TestCase

assert\_same “hello”, “hello”  
== END ==

doesn’t seem to work. It says:  
test.rb:8: undefined method `assert\_same’ for main:Object  
(NoMethodError)

Surely the includes include this method?

I am new to ruby. Googling just brings up billions of irrelevant  
results. The more I google the more confusion occurs. If someone can  
just give me a quick heads up to what is wrong then I can sort this out.

Thanks!

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [April 20, 2011, 3:15am UTC](https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986/2 "2011-04-20T03:15:43Z")

</div>

I added this line:  
require ‘test/unit/assertions’

Which is different from:  
include Test::Unit::Assertions

In PHP require and include are the same hence the confusion having come  
from PHP. I don’t understand the difference between require and include  
in ruby yet, but at least my problem is sorted 🙂

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [April 20, 2011, 10:45am UTC](https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986/3 "2011-04-20T10:45:49Z")

</div>

Mike RegistrationErr wrote in post #993884:

> I added this line:  
> require ‘test/unit/assertions’
> 
> Which is different from:  
> include Test::Unit::Assertions
> 
> In PHP require and include are the same hence the confusion having come  
> from PHP. I don’t understand the difference between require and include  
> in ruby yet, but at least my problem is sorted 🙂

“require” means “go and load this ruby source file, if you’ve not loaded  
it before”

“include” means (roughly) “add the methods in that module into the  
current class”

“extend” means (roughly) “add the methods in that module into the  
current object”

module Foo  
def bar  
puts “I am bar!”  
end  
end

bar # doesn’t work  
include Foo  
bar # now it works

a = “Hello”  
a.extend Foo  
a.bar # this works too

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [April 20, 2011, 7:21pm UTC](https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986/4 "2011-04-20T19:21:28Z")

</div>

Mike RegistrationErr wrote in post #993883:

> The script (test.rb):
> 
> == START ==  
> require ‘rubygems’  
> require ‘firewatir’
> 
> include Test::Unit  
> include Test::Unit::Assertions  
> include Test::Unit::TestCase
> 
> assert\_same “hello”, “hello”  
> == END ==
> 
> doesn’t seem to work. It says:  
> test.rb:8: undefined method `assert\_same’ for main:Object  
> (NoMethodError)
> 
> Surely the includes include this method?
> 
> I am new to ruby.

In ruby ‘hello’ and ‘hello’ are not _the same_. Quotes serve as a  
String constructor, and those are two different objects. For instance,

a = ‘hello’  
b = ‘hello’

if a.equal?(b)  
puts ‘yes’  
else  
puts ‘no’  
end

–output:–  
no

That is the same test that assert\_same uses. Instead, you may wish to  
use assert\_equal, which tests whether two strings contain the same  
characters:

a = ‘hello’  
b = ‘hello’

if a == b  
puts ‘yes’  
else  
puts ‘no’  
end

–output:–  
yes

> In PHP require and include are the same hence the  
> confusion having come

ruby has no equivalent of the include() in php.  
from PHP.

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [April 20, 2011, 10:36pm UTC](https://www.ruby-forum.com/t/assert-name-doesnt-exist-as-a-function/205986/5 "2011-04-20T22:36:30Z")

</div>

7stud – wrote in post #994086:

> > test.rb:8: undefined method `assert\_same’ for main:Object  
> > (NoMethodError)
> > 
> > Surely the includes include this method?
> > 
> > I am new to ruby.
> 
> In ruby ‘hello’ and ‘hello’ are not _the same_. Quotes serve as a  
> String constructor, and those are two different objects.

That would explain it if assert\_same raised an AssertionFailedError. But  
it does not explain the NoMethodError 🙂

It works as expected for me if I require test/unit explicitly:

irb(main):001:0\> require ‘test/unit’  
=\> true  
irb(main):002:0\> include Test::Unit::Assertions  
=\> Object  
irb(main):003:0\> assert\_same “hello”,“hello”  
Test::Unit::AssertionFailedError: \<“hello”\>  
with id \<70122845327940\> expected to be equal? to  
\<“hello”\>  
with id \<70122845327920\>.  
from /usr/lib/ruby/1.8/test/unit/assertions.rb:48:in `assert_block' from /usr/lib/ruby/1.8/test/unit/assertions.rb:495:in `\_wrap\_assertion’  
from /usr/lib/ruby/1.8/test/unit/assertions.rb:46:in `assert_block' from /usr/lib/ruby/1.8/test/unit/assertions.rb:249:in `assert\_same’  
from (irb):3  
from /usr/lib/ruby/1.8/optparse.rb:311

I am using ruby 1.8.7p299 under Ubuntu 10.10 x86\_64. The OP didn’t  
specify their platform. Maybe they are running an old version of Ruby  
which doesn’t include assert\_same? Or possibly require ‘firewatir’ is  
defining the Test::Unit::\* constants, without actually requiring  
test/unit/assertions?
