# Test::unit

**URL:** <https://www.ruby-forum.com/t/test-unit/160580>\
**Category:** Rails\
**Created:** [February 18, 2009, 12:01am UTC](https://www.ruby-forum.com/t/test-unit/160580 "2009-02-18T00:01:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 12:01am UTC](https://www.ruby-forum.com/t/test-unit/160580/1 "2009-02-18T00:01:03Z")

</div>

Hello,

im trying since a few days with unit test.  
my problem is, i have write a test for 7 validates\_presence\_of and its  
ok.  
then i have write 2 validates\_uniqueness\_of and its ok.  
then i have write 1 test for validates\_format\_of :postcode, :with =\>  
/^\d{5}$/  
its also ok.  
if i have write a test for validates\_format\_of :street, :with =\>  
/^([[:alpha:]]+\s)+\d\*$/ like this:

` def test_street_format
   ok = ["on the road 7", "my street"]
   notok = ["8ue kd9", "die 8.straÃŸe 9"]
   ok.each do |name|
     sp = Me.new(:name => "MyString", :contact_person => "My person",
:location => "Mybla", :postcode => "12345", :street => name, :password
=> "MyStkdju", :email => "removed_email_address@domain.invalid")
     assert sp.valid? #, sp.errors.full_messages
   notok.each do |name|
     sp = Me.new(:name => "MyString", :contact_person => "My person",
:location => "Mybla", :postcode => "12345", :street => name, :password
=> "MyStkdju", :email => "removed_email_address@domain.invalid")
     assert !sp.valid?
     end
   end
 end`  
i became failures like this

```auto
1) Failure:
test_postcode_format(MeTest):
<false> is not true.

2) Failure:
test_street_format(MeTest):
<false> is not true.

Finished in 0,139 seconds.
8 tests, 2 failures, 0 errors
```

I don’t know whats wrong.  
please help.

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 12:27am UTC](https://www.ruby-forum.com/t/test-unit/160580/2 "2009-02-18T00:27:32Z")

</div>

On Feb 17, 11:01 pm, Hello G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> /^([[:alpha:]]+\s)+\d\*$/ like this:  
> Looks to me like this regexp means that “my street” cannot match  
> (since you’ve said that each of your words must be finished by a space  
> (so i would expect "my street " to pass)).  
> Other than that you need to do some detective work: what line is it  
> failing on ? with what input ? which validations are failing etc.

Fred

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 3:20am UTC](https://www.ruby-forum.com/t/test-unit/160580/3 "2009-02-18T03:20:51Z")

</div>

> > then i have write 1 test for validates\_format\_of :postcode, :with =\>  
> > /^\d{5}$/  
> > its also ok.

> Looks to me like this regexp means that “my street” cannot match  
> (since you’ve said that each of your words must be finished by a space  
> (so i would expect "my street " to pass)).  
> Other than that you need to do some detective work: what line is it  
> failing on ? with what input ? which validations are failing etc.

Next advice: Comment out every test that fails, then write a test like  
this:

assert\_match ‘12348’, /^\d{5}/

If you have trouble with a high-level test, scratch it and write a  
low-level  
one; one that only targets the details. In this case, you are not even  
testing  
the actual validator; you are using the testing to learn which regexps  
work.

–  
Phlip  
[Radar – O’Reilly](http://broadcast.oreilly.com/2009/02/merb-mind-maps.html)

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/test-unit/160580/4 "2009-02-18T15:15:50Z")

</div>

Frederick C. wrote:

> On Feb 17, 11:01ï¿½pm, Hello G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > /^([[:alpha:]]+\s)+\d\*$/ like this:  
> > Looks to me like this regexp means that “my street” cannot match  
> > (since you’ve said that each of your words must be finished by a space  
> > (so i would expect "my street " to pass)).  
> > Other than that you need to do some detective work: what line is it  
> > failing on ? with what input ? which validations are failing etc.
> 
> Fred

hello,

my test for validates\_format\_of :street, :with =\>  
/^[[:alpha:]]+\s{1}_[[:alpha:]])_\d\*$/ is this:

def test\_street\_format  
ok = [“on road 7”, “yourstreet”]  
notok = [“8ue kd9”, “die 8.straÃŸe 9”]  
ok.each do |name|  
sp = Me.new(:name =\> “MyString”, :contact\_person =\> “My person”,  
:location =\> “Mybla”, :postcode =\> “12345”, :street =\> name, :email =\>  
“removed\_email\_address@domain.invalid”)  
assert sp.valid? #, sp.errors.full\_messages  
notok.each do |name|  
sp = Me.new(:name =\> “MyString”, :contact\_person =\> “My person”,  
:location =\> “Mybla”, :postcode =\> “12345”, :street =\> name, :email =\>  
“removed\_email\_address@domain.invalid”)  
assert !sp.valid?  
end  
end  
end

is my test not ok?

i hope someone can help me please

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/test-unit/160580/5 "2009-02-18T21:48:15Z")

</div>

Phlip wrote:

> Did you also write a simpler test with only the rexexps and their  
> targets in it?
> 
> If a test that starts too far from your tested code is hard to work  
> with, that  
> is a sign you need more support from a lower-level test.

Hello,

my English isnt so good, sorry. I dont understand everything you wrote.  
can you give me an example for a simpler test for my situation?

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/test-unit/160580/6 "2009-02-18T16:05:34Z")

</div>

Hello G. wrote:

> my test for validates\_format\_of :street, :with =\>  
> /^[[:alpha:]]+\s{1}_[[:alpha:]])_\d\*$/ is this:

> ```
> assert !sp.valid?
> 
> ```
> 
> is my test not ok?

Did you also write a simpler test with only the rexexps and their  
targets in it?

If a test that starts too far from your tested code is hard to work  
with, that  
is a sign you need more support from a lower-level test.

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 18, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/test-unit/160580/7 "2009-02-18T23:00:01Z")

</div>

Hello G. wrote:

> my English isnt so good, sorry. I dont understand everything you wrote.  
> can you give me an example for a simpler test for my situation?

in your model:

VALIDATE = /@#$%^/  
validates\_format\_of :snoo, VALIDATE

in the test:

def test\_validate  
assert\_match Model::VALIDATE, ‘something’  
end

That’s all. Don’t build everything just to test the Regexp - just test  
it directly!

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 19, 2009, 10:24am UTC](https://www.ruby-forum.com/t/test-unit/160580/8 "2009-02-19T10:24:02Z")

</div>

Phlip wrote:

> Hello G. wrote:
> 
> > my English isnt so good, sorry. I dont understand everything you wrote.  
> > can you give me an example for a simpler test for my situation?
> 
> in your model:
> 
> VALIDATE = /@#$%^/  
> validates\_format\_of :snoo, VALIDATE
> 
> in the test:
> 
> def test\_validate  
> assert\_match Model::VALIDATE, ‘something’  
> end
> 
> That’s all. Don’t build everything just to test the Regexp - just test  
> it directly!

ok thank you. i try this:  
in model:  
validates\_format\_of :street, :with =\>  
/^[[:alpha:]]+\s{1}_[[:alpha:]])_\d\*$/

in test:  
def test\_street\_try  
assert\_match(/^[[:alpha:]]+\s{1}_[[:alpha:]])_\d\*$/, ‘The Road 9’)  
end

but i become this:  
Finished in 0,0 seconds.  
0 tests, 0 failures, 0 errors

whats wrong now? i dont know

---

<div class="post-metadata">

**Author:** ![jobber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@jobber](https://www.ruby-forum.com/u/jobber)\
**Post date:** [February 19, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/test-unit/160580/9 "2009-02-19T20:34:53Z")

</div>

Hello G. wrote:

> ok thank you. i try this:  
> in model:  
> validates\_format\_of :street, :with =\>  
> /^[[:alpha:]]+\s{1}_[[:alpha:]])_\d\*$/
> 
> in test:  
> def test\_street\_try  
> assert\_match(/^[[:alpha:]]+\s{1}_[[:alpha:]])_\d\*$/, ‘The Road 9’)  
> end

The // regexp in the validation should be the same one passed to the  
test.  
That’s so when you change the validation, the test will still cover it.

That’s why I suggested putting the regexp into a constant.

> but i become this:  
> Finished in 0,0 seconds.  
> 0 tests, 0 failures, 0 errors

You are not running any tests, because your assertion should have  
counted.

Try with this:

assert false

in a test. If your tests don’t break, then you are not running them!

–  
Phlip
