[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Creating unit-test where RE required

RichardOnRails

1/18/2008 5:16:00 AM

Hi All,

I have a Search class with a :nameRE setter which may validly be
either nil or a regular expression.

I setup the unit test:

def test_002_IfNameGivenThenRE
@search.nameRE = '/\.rb$'
assert_raise(RuntimeError, @search.run)
end

search.run invokes validate which in turn invokes:

fail "'#{@nameRE.source}' is not a valid regular expression for
nameRE!" if
@nameRE && Regexp.new(@nameRE).class != Regexp

That lead to the unit test result:

2) Failure:
test_002_IfNameGivenThenRE(TestValidation) [UnitTestSearch.rb:23]:
<RuntimeError> exception expected but was
Class: <LocalJumpError>
Message: <"no block given">

So I thought I should change the assert to:

@search.nameRE = '/\.rb$'
assert_raise(LocalJumpError, @search.run)

but that gave me the test result: Only other failures but none for
the nameRE.

Did I present my question clearly? Any ideas?

Thanks in Advance,
Richard
2 Answers

Paul McMahon

1/18/2008 5:57:00 AM

0

You are using assert_raise improperly. It should be

assert_raise(RuntimeError) do
@search.run
end

see http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.ht...

RichardOnRails

1/19/2008 3:08:00 AM

0

On Jan 18, 12:57 am, Paul McMahon <p...@ubit.com> wrote:
> You are using assert_raise improperly. It should be
>
> assert_raise(RuntimeError) do
> @search.run
> end
>
> see http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.ht...

Hi Paul,

> http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.ht...

Thanks for the reference.

OK, I've got it: here are the first two validation expressions in
Search#validate:

fail "Starting search directory is required: " +
(@dirname ? ('"' + @dirname + '"') : 'nil') +
" is not a directory" unless
@dirName && File.directory?(@dirName)

fail "'#{@nameRE}' is not a valid regular expression for nameRE!" if
@nameRE && !@nameRE.instance_of?(Regexp)

The corresponding unit tests (all of which succeed) are:

def test_000_DirRequired
@search.dirName = nil
assert_raise(RuntimeError) {@search.run}
end

def test_001_DirMustBeValid
@search.dirName = '../no_such_dir'
assert_raise(RuntimeError) {@search.run}
end

def test_002_IfNameGivenThenRE
@search.nameRE = '/\.rb$'
assert_raise(RuntimeError) {@search.run}
end


And everything works as expected when actually using the setters for a
real invocation of Search.

Thanks for your help.

Best wishes,
Richard