# Creating unit-test where RE required

**URL:** <https://www.ruby-forum.com/t/creating-unit-test-where-re-required/126428>\
**Category:** Ruby\
**Created:** [January 18, 2008, 6:21am UTC](https://www.ruby-forum.com/t/creating-unit-test-where-re-required/126428 "2008-01-18T06:21:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [January 18, 2008, 6:21am UTC](https://www.ruby-forum.com/t/creating-unit-test-where-re-required/126428/1 "2008-01-18T06:21:32Z")

</div>

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:

1. Failure:  
test\_002\_IfNameGivenThenRE(TestValidation) [UnitTestSearch.rb:23]:  
 exception expected but was  
Class:   
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

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [January 18, 2008, 6:58am UTC](https://www.ruby-forum.com/t/creating-unit-test-where-re-required/126428/2 "2008-01-18T06:58:21Z")

</div>

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.html#M006692](http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.html#M006692)

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [January 19, 2008, 4:11am UTC](https://www.ruby-forum.com/t/creating-unit-test-where-re-required/126428/3 "2008-01-19T04:11:56Z")

</div>

On Jan 18, 12:57 am, Paul McMahon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.html#M006692](http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.html#M006692)

Hi Paul,

> [http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.html#M006692](http://www.ruby-doc.org/core/classes/Test/Unit/Assertions.html#M006692)

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
