[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Assertions Testing in irb

Bharat Ruparel

2/19/2007 5:50:00 PM

I am working through the Everyday Scripting With Ruby book and am trying
to run assertion tests in irb.

If I type in:
irb(main):005:0> require 'test/unit'
=> true
As you can see above it returns true. I am assuming that this will load
the unit test methods in irb. However, if I type in an assertion test
as follows:

irb(main):006:0>
assert_equal('2005-03-4',svn_date(Time.local(2005,3,4)))
NoMethodError: undefined method `assert_equal' for main:Object
from (irb):6
from :0

It fails as above.

I have also tried to use the load command as:

load "Test::Unit" but that does not seem to work either. See below:

irb(main):008:0> load "Test::Unit"
LoadError: no such file to load -- Test::Unit
from (irb):8:in `load'
from (irb):8
from :0

What do I need to do to get the assertion methods working in irb?

Thanks.
Bharat

--
Posted via http://www.ruby-....

32 Answers

Austin Ziegler

2/19/2007 5:56:00 PM

0

On 2/19/07, Bharat Ruparel <bruparel@mercury.com> wrote:
> I am working through the Everyday Scripting With Ruby book and am trying
> to run assertion tests in irb.
>
> If I type in:
> irb(main):005:0> require 'test/unit'
> => true
> As you can see above it returns true. I am assuming that this will load
> the unit test methods in irb. However, if I type in an assertion test
> as follows:

No. It loads the Test::Unit framework. This gives you
Test::Unit::TestCase (the class where you use unit test assertions),
but you can include the assertions anywhere:

include Test::Unit::Assertions

That will include them in Object and you can use them that way.

I'm not sure I recommend this, though.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Bharat Ruparel

2/19/2007 6:04:00 PM

0

Hello Austin,
You wrote:

>
> include Test::Unit::Assertions
>
> That will include them in Object and you can use them that way.
>
> I'm not sure I recommend this, though.
>
> -austin

I did that and it works! Thanks. Why are you saying that you have
doubts about recommending something that gets the job done?

I have a side question. I am not sure when to use:

1. load
2. require
3. include

it irb. Can someone give a simple set of guidelines for that?

Thanks.
Bharat

--
Posted via http://www.ruby-....

SonOfLilit

2/19/2007 6:08:00 PM

0

Things that work don't necessarily mean good design.

load is rarely used, require is to include libraries or code files,
include is to mixin a module and has nothing to do with the previous
two.

Plug: The RubyMentor project is looking for people to help!

http://rubymentor.rubyforge.org/wiki/wiki.p...

On 2/19/07, Bharat Ruparel <bruparel@mercury.com> wrote:
> Hello Austin,
> You wrote:
>
> >
> > include Test::Unit::Assertions
> >
> > That will include them in Object and you can use them that way.
> >
> > I'm not sure I recommend this, though.
> >
> > -austin
>
> I did that and it works! Thanks. Why are you saying that you have
> doubts about recommending something that gets the job done?
>
> I have a side question. I am not sure when to use:
>
> 1. load
> 2. require
> 3. include
>
> it irb. Can someone give a simple set of guidelines for that?
>
> Thanks.
> Bharat
>
> --
> Posted via http://www.ruby-....
>
>

Pit Capitain

2/19/2007 6:28:00 PM

0

Bharat Ruparel schrieb:
> I have a side question. I am not sure when to use:
>
> 1. load
> 2. require
> 3. include
>
> it irb. Can someone give a simple set of guidelines for that?

Bharat, in addition to what Aur said, let me explain it again:

#require looks for a *file* in the Ruby load path and executes it, if it
hasn't done so already. This is the normal way to load libraries.

#load is rarely used, as Aur said. The main difference to #require is
that the file is executed everytime the #load method is called, not only
the first time.

The files executed with #require and #load normally define or extend
classes and modules.

#include allows you to include the methods of *modules* into your
current class. Often the included modules come from previously required
files, so you often see a sequence like this:

require <name-of-file>

class MyClass
include <module-defined-in-file-loaded-above>
end

Regards,
Pit

Bharat Ruparel

2/19/2007 6:30:00 PM

0

SonOfLilit wrote:
> Things that work don't necessarily mean good design.
>
> load is rarely used, require is to include libraries or code files,
> include is to mixin a module and has nothing to do with the previous
> two.
>
> Plug: The RubyMentor project is looking for people to help!
>
> http://rubymentor.rubyforge.org/wiki/wiki.p...

Ok, then there has to be "recommended" way to "invoke" the unit testing
functionality in irb. I find irb extremely useful in learning ruby.
Also, I am loading file(s) in irb all the time, so "load is rarely used"
does not make much sense to me. perhaps you meant to say that "load" is
rarely used in a regular script? Also, I am a bit puzzled over why I
cannot "load" a ruby built-in fuctionality into irb (such as unit
testing). Note that I don't have a particular preference for using load
versus require. However, to me, load seems to work better in irb and
require is what I use in a regular script.

Please comment.
Thanks.
Bharat

--
Posted via http://www.ruby-....

Austin Ziegler

2/19/2007 6:40:00 PM

0

On 2/19/07, Bharat Ruparel <bruparel@mercury.com> wrote:
> > include Test::Unit::Assertions
> >
> > That will include them in Object and you can use them that way.
> >
> > I'm not sure I recommend this, though.
> >
> > -austin
>
> I did that and it works! Thanks. Why are you saying that you have
> doubts about recommending something that gets the job done?

I don't recommend using Test::Unit assertions in IRB.

> I have a side question. I am not sure when to use:
>
> 1. load

This will load a Ruby source (.rb or dynamic library) regardless of
whether it's been included before. It does not have automatic
resolution (e.g., require 'foo/bar' would need to be load
'lib/foo/bar.rb'), if lib/foo/bar.rb is the appropriate relative path
from the current working directory.

> 2. require

This will load a Ruby source once and only once. This is most common.

> 3. include

This is the mechanism used for mixing in modules. This is wholly
unrelated to require and load.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Austin Ziegler

2/19/2007 6:40:00 PM

0

On 2/19/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> Things that work don't necessarily mean good design.
>
> load is rarely used

This is not true. It has specialized use, which is different than "rarely used."

Rails uses it during development mode all the time, IIRC.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

SonOfLilit

2/19/2007 6:42:00 PM

0

I meant that #require is in general preferred over #load by
experienced ruby programmers.

On 2/19/07, Bharat Ruparel <bruparel@mercury.com> wrote:
> SonOfLilit wrote:
> > Things that work don't necessarily mean good design.
> >
> > load is rarely used, require is to include libraries or code files,
> > include is to mixin a module and has nothing to do with the previous
> > two.
> >
> > Plug: The RubyMentor project is looking for people to help!
> >
> > http://rubymentor.rubyforge.org/wiki/wiki.p...
>
> Ok, then there has to be "recommended" way to "invoke" the unit testing
> functionality in irb. I find irb extremely useful in learning ruby.
> Also, I am loading file(s) in irb all the time, so "load is rarely used"
> does not make much sense to me. perhaps you meant to say that "load" is
> rarely used in a regular script? Also, I am a bit puzzled over why I
> cannot "load" a ruby built-in fuctionality into irb (such as unit
> testing). Note that I don't have a particular preference for using load
> versus require. However, to me, load seems to work better in irb and
> require is what I use in a regular script.
>
> Please comment.
> Thanks.
> Bharat
>
> --
> Posted via http://www.ruby-....
>
>

Austin Ziegler

2/19/2007 6:45:00 PM

0

On 2/19/07, Pit Capitain <pit@capitain.de> wrote:
> #include allows you to include the methods of *modules* into your
> current class. Often the included modules come from previously required
> files, so you often see a sequence like this:
>
> require <name-of-file>
>
> class MyClass
> include <module-defined-in-file-loaded-above>
> end

Note as well that the name-of-file need not have anything to do with
the modules or classes using in the include statement.

Strictly speaking, require and load work on resources. Just because
current Ruby uses files does not mean that other mechanisms can't be
used; some folks have extended #require with open-uri and there's
nothing that stops anyone from looking in tar files or zip files for
files to load.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Austin Ziegler

2/19/2007 6:49:00 PM

0

On 2/19/07, Bharat Ruparel <bruparel@mercury.com> wrote:
> Ok, then there has to be "recommended" way to "invoke" the unit testing
> functionality in irb.

Yes. The recommended way is "don't." Unit tests are things that are
designed to be repeatable and related to classes that you're building.
Simply using the assertions gains you absolutely nothing with respect
to using IRB itself.

> I find irb extremely useful in learning ruby.
> Also, I am loading file(s) in irb all the time, so "load is rarely used"
> does not make much sense to me.

Use "load" when you want to reread the classes you're working with
from disk. Otherwise, forget that it exists.

> Also, I am a bit puzzled over why I
> cannot "load" a ruby built-in fuctionality into irb (such as unit
> testing).

You tried "load Test::Unit" (or "load 'Test::Unit'"); this won't work
because load doesn't work that way. Doing 'load "test/unit"' won't
work, either, because Ruby doesn't dynamically resolve the extension.
Doing 'load "test/unit.rb"' does.

> Note that I don't have a particular preference for using load
> versus require.

You should.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca