# Creating unit-test where RE required

**URL:** https://rubytalk.org/t/creating-unit-test-where-re-required/43869
**Category:** ruby-talk
**Created:** [18 January 2008 05:20 UTC](https://rubytalk.org/t/creating-unit-test-where-re-required/43869 "2008-01-18T05:20:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)
#### Post date: [18 January 2008 05:20 UTC](https://rubytalk.org/t/creating-unit-test-where-re-required/43869/1 "2008-01-18T05:20:04Z")

</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:

&nbsp;&nbsp;def test\_002\_IfNameGivenThenRE  
&nbsp;&nbsp;&nbsp;&nbsp;@search.nameRE = '/\.rb$'  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(RuntimeError, @search.run)  
&nbsp;&nbsp;end

search.run invokes validate which in turn invokes:

&nbsp;&nbsp;&nbsp;&nbsp;fail "'#{@nameRE.source}' is not a valid regular expression for  
nameRE!" if  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@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:

&nbsp;&nbsp;@search.nameRE = '/\.rb$'  
&nbsp;&nbsp;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: ![Paul\_McMahon1](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@Paul\_McMahon1](https://rubytalk.org/u/Paul_McMahon1)
#### Post date: [18 January 2008 05:57 UTC](https://rubytalk.org/t/creating-unit-test-where-re-required/43869/2 "2008-01-18T05:57:01Z")

</div>

You are using assert\_raise improperly. It should be

assert\_raise(RuntimeError) do  
&nbsp;&nbsp;&nbsp;@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: ![Richard3](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@Richard3](https://rubytalk.org/u/Richard3)
#### Post date: [19 January 2008 03:10 UTC](https://rubytalk.org/t/creating-unit-test-where-re-required/43869/3 "2008-01-19T03:10:19Z")

</div>

> You are using assert\_raise improperly. It should be
> 
> assert\_raise(RuntimeError) do  
> &nbsp;&nbsp;&nbsp;@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  
&nbsp;&nbsp;&nbsp;&nbsp;@dirName && File.directory?(@dirName)

fail "'#{@nameRE}' is not a valid regular expression for nameRE!" if  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@nameRE && !@nameRE.instance\_of?(Regexp)

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

&nbsp;&nbsp;def test\_000\_DirRequired  
&nbsp;&nbsp;&nbsp;&nbsp;@search.dirName = nil  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(RuntimeError) {@search.run}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_001\_DirMustBeValid  
&nbsp;&nbsp;&nbsp;&nbsp;@search.dirName = '../no\_such\_dir'  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(RuntimeError) {@search.run}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_002\_IfNameGivenThenRE  
&nbsp;&nbsp;&nbsp;&nbsp;@search.nameRE = '/\.rb$'  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(RuntimeError) {@search.run}  
&nbsp;&nbsp;end

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

Thanks for your help.

Best wishes,  
Richard

> **···**
>
> On Jan 18, 12:57 am, Paul McMahon \<p...@ubit.com\> wrote:
