# Using assert() in a loop

**URL:** <https://www.ruby-forum.com/t/using-assert-in-a-loop/54280>\
**Category:** Ruby\
**Created:** [February 28, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/using-assert-in-a-loop/54280 "2006-02-28T15:21:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![toby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@toby](https://www.ruby-forum.com/u/toby)\
**Post date:** [February 28, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/using-assert-in-a-loop/54280/1 "2006-02-28T15:21:36Z")

</div>

Hello everybody

I hope this question isn’t answered somewhere else, but the search was  
disabled so i hope that excuses me.

I have been introducted to Ruby thorugh the need to write Watir tests to  
do functional testing. I have an array of URLs that i wish to check and  
a loop to go through them. I use the assert() function to find out  
whether a particular link has scrolled to the correct position. If the  
test fails (ie assert(false)) ruby breaks out of the loop and doesn’t  
check the rest of the urls.

I have added a begin/rescue statement which stops ruby breaking out the  
loops but Watir then reports zero failures.

Code looks like this:

```auto
def test_href_Exists

		href_start = 
"http://localhost/fc_codebase/_web/?urn=com.firstconsult/1/101/1010022"

		link_arr = {
		"Description" => "198"
		}

		href_addr = [
		"%23ah_70002::th_unused",
		"%23ah_70089::th_",
		"#ah_70002::th_unused",
		"%23ah_70002::",
		"#ah_70002::",
		"%23ah_70002::",
		"#ah_70002::",
		"%23ah_70002::t)(*&^%$",
		"#ah_70002::t)(*&^%$",
		"%23ah_70002::%23tyh_888",
		"#ah_70002::#tyh_888",
		"%23ah_70567::th_unsed",
		"%23ah_70567::th_70002",
		"%23ah_70567::th_unused",
		"%23ah_unused::th_unused",
		"#ah_unused::th_unused",
		"#ah_70567::th_",
		"%23ah_70567::th_",
		"%23ah_70567::th_70002&%",
		"#ah_70567::th_70002%%",
		]

		$ie.goto(href_start)

		link_arr.each do |key, value|
			href_addr.each do |href_arr_val|

				href_complete = href_start + href_arr_val
				$ie.text_field(:name, "id_href").set(href_complete)
				$ie.link(:text, key).click
				puts $ie.text_field(:name, "id_href").getContents() + " " + 
href_arr_val

				begin

				   assert(result);

				rescue => e

				    puts "*FAILED*." + e.message
				end

			end
		end
```

I am fairly sure there is a simple explaination but nothing i have tried  
works so far this week.

Any help would be gratfully recieved

thanks loads  
toby

---

<div class="post-metadata">

**Author:** ![toby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@toby](https://www.ruby-forum.com/u/toby)\
**Post date:** [February 28, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/using-assert-in-a-loop/54280/2 "2006-02-28T19:15:39Z")

</div>

As with many of these problems, hours of head banging are solved with a  
single line.

i used the begin/rescue code but added a watir function call that  
updates the failures. this works perfectly and returns a full list of  
everything that has failed back to the ci server.

begin  
assert(result);  
rescue =\> e  
add\_failure(1)  
puts “_FAILED_.” + e.message  
end

I can see the logic in ‘if one fails the whole thing fails’, i just  
think it’s good practice to see exactly what has failed rather than just  
the tip of the iceberg. I’d forgotten how frustrating it is to learn a  
new language, loving the challenge tho and getting to love ruby.

---

<div class="post-metadata">

**Author:** ![toby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@toby](https://www.ruby-forum.com/u/toby)\
**Post date:** [February 28, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/using-assert-in-a-loop/54280/3 "2006-02-28T17:01:01Z")

</div>

Toby wrote:

> loop and doesn’t check the rest of the urls.  
> “[http://localhost/fc\_codebase/\_web/?urn=com.firstconsult/1/101/1010022](http://localhost/fc_codebase/_web/?urn=com.firstconsult/1/101/1010022)”  
> “#ah\_70002::”,  
> “#ah\_unused::th\_unused”,
> 
> rescue =\> e
> 
> Any help would be gratfully recieved

Well, the test fails as soon as a single URL fails. That’s the reason  
for  
the behavior you’re seeing. If you want a single error message that  
lists  
all failues the way to go is to collect all failures somehow (Array,  
Hash)  
and have it fail only if there is at least one error.

HTH

Kind regards

```
robert

```
