# Newbie: Control flow of tests using 'define\_method'

**URL:** <https://www.ruby-forum.com/t/newbie-control-flow-of-tests-using-define-method/197195>\
**Category:** Ruby\
**Created:** [April 13, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/newbie-control-flow-of-tests-using-define-method/197195 "2010-04-13T12:53:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [April 13, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/newbie-control-flow-of-tests-using-define-method/197195/1 "2010-04-13T12:53:37Z")

</div>

I am very new to development and even more in the dark when it comes to  
Ruby, but I am still having fun. 😉 I also apologise for the long  
snippets of code in this post.

I am trying to test a database migration using Ruby and the built in  
Test framework.

I am stuck however on a particular problem and it relates to control  
flow and the dynamic use of ‘define\_method’ to create multiple Tests if  
an assert\_equal ‘fails’ thus jumping out of a loop and stopping all  
future tests.

I found what I thought was the solution from this post:

[http://www.ruby-forum.com/topic/204730](http://www.ruby-forum.com/topic/204730)

One I notice however is it will not execute the define\_method block  
until it has executed all of the exterior loops.

My code is as follows:

class TestSuite \< Test::Unit::TestCase

#Build a hash of Source Database columns and target Database columns

```
companyRRAccessToBeacon = Hash.new
companyRRAccessToBeacon['company_id'] = 'ID'
cmpanyRRAccessToBeacon['ultimate_parent_id'] = 'GlobalUltimateID'
companyRRAccessToBeacon['fullname'] = 'Name'
companyRRAccessToBeacon['turnover_curr'] = 'TurnoverCurrencyTypeID'
companyRRAccessToBeacon['turnover'] = 'TurnoverAmount'
companyRRAccessToBeacon['u_size_code'] = 'CorporateSizeTypeID'
companyRRAccessToBeacon['corp_status_id'] =

```

‘CorporateOwnershipTypeID’  
companyRRAccessToBeacon[‘created\_user’] = ‘RowCreatedBy’  
companyRRAccessToBeacon[‘created\_datetime’] = ‘RowCreatedDate’  
companyRRAccessToBeacon[‘last\_modification\_by’] = ‘RowModifiedBy’  
companyRRAccessToBeacon[‘last\_modification\_date’] =  
‘RowModifiedDate’  
companyRRAccessToBeacon[‘off\_limits\_id’] = ‘OffLimitsID’

```
#Connect to the RRAccess and Beacon databases
rraccesshandle = connectrraccessdb
beaconhandle = connectbeacondb

#Companyrowdata stores the Data Driven tests used for the test

```

below.  
companyrowData =  
getXlsRowData(“C:\Users\ecucropc\Documents\Visual Studio  
2008\Projects\DataSynchronisation\DataSynchronisation\Company  
Data.xls”, “Company”, “A1:L3”)

```
#Loop through each record returned in the Data Driven Test set.
companyrowData.each do | companydata |
  puts "In #{companydata} loop"
  #If no Company records are returned then run the test.
  companydata["company_id"] != ""
  recordtofetch = companydata["company_id"]

  #Get the common RRAccess Company columns
  sqlRRaccessValues = (getDBValue(rraccesshandle, "select

```

company\_id, ultimate\_parent\_id, fullname, turnover\_curr, turnover,  
U\_Size\_Code, corp\_status\_id, created\_user, created\_datetime,  
last\_modification\_by, last\_modification\_date, off\_limits\_id from  
RRAccessLive.Company WHERE Company\_id = ?", recordtofetch))

```
  #Get the common Beacon Company columns
  sqlBeaconValues = (getDBValue(beaconhandle, "select ID,

```

GlobalUltimateID, Name, TurnoverCurrencyTypeID, TurnoverAmount,  
CorporateSizeTypeID, CorporateOwnershipTypeID, RowCreatedBy,  
RowCreatedDate, RowModifiedBy, RowModifiedDate, OffLimitsID from  
BeaconLive.Company WHERE ID = ?", recordtofetch))

```
  #For each HashMap pair get the values from the RRAccess and Beacon

```

databases and then  
#loop through and perform the comparison test.

```
  companyRRAccessToBeacon.each {|key, value|
    puts "Inside each loop #{companyRRAccessToBeacon[key]}"
      rrAccessColumnValue = sqlRRaccessValues[key]
      rrBeaconColumnValue = sqlBeaconValues[value]

    #This dynamic define_method is used to create many tests.

    define_method ("test_company_" + companyRRAccessToBeacon[key])

```

do  
puts “Inside assert\_equal loop for  
test\_company\_#{companyRRAccessToBeacon[key]}”  
assert\_equal sqlRRaccessValues[key], sqlBeaconValues[value]  
end  
}  
end  
end

The output of this is as follows:

In off\_limits\_idFturnover\_currUSDlast\_modification\_date2008/06/25  
11:10:00corp\_status\_idQUOcreated\_user2000734ultimate\_parent\_id1fullnameAAR  
CorporationU\_Size\_Code1last\_modification\_by40000347turnover1061169created\_datetime1988/10/17  
00:00:00company\_id1 loop  
Inside each loop OffLimitsID  
Inside each loop TurnoverCurrencyTypeID  
Inside each loop RowModifiedDate  
Inside each loop CorporateOwnershipTypeID  
Inside each loop RowCreatedBy  
Inside each loop GlobalUltimateID  
Inside each loop Name  
Inside each loop CorporateSizeTypeID  
Inside each loop RowModifiedBy  
Inside each loop TurnoverAmount  
Inside each loop RowCreatedDate  
Inside each loop ID  
In off\_limits\_idCturnover\_currUSDlast\_modification\_date2009/12/01  
14:51:00corp\_status\_idQUOcreated\_user9999999ultimate\_parent\_id2fullnameAbbott  
LaboratoriesU\_Size\_Code1last\_modification\_by40001040turnover22476322created\_datetime1978/01/01  
00:00:00company\_id2 loop  
Inside each loop OffLimitsID  
Inside each loop TurnoverCurrencyTypeID  
Inside each loop RowModifiedDate  
Inside each loop CorporateOwnershipTypeID  
Inside each loop RowCreatedBy  
Inside each loop GlobalUltimateID  
Inside each loop Name  
Inside each loop CorporateSizeTypeID  
Inside each loop RowModifiedBy  
Inside each loop TurnoverAmount  
Inside each loop RowCreatedDate  
Inside each loop ID  
In off\_limits\_idFturnover\_currUSDlast\_modification\_date2008/06/25  
11:10:00corp\_status\_idQUOcreated\_user2000734ultimate\_parent\_id1fullnameAAR  
CorporationU\_Size\_Code1last\_modification\_by40000347turnover1061169created\_datetime1988/10/17  
00:00:00company\_id1 loop  
Inside each loop OffLimitsID  
Inside each loop TurnoverCurrencyTypeID  
Inside each loop RowModifiedDate  
Inside each loop CorporateOwnershipTypeID  
Inside each loop RowCreatedBy  
Inside each loop GlobalUltimateID  
Inside each loop Name  
Inside each loop CorporateSizeTypeID  
Inside each loop RowModifiedBy  
Inside each loop TurnoverAmount  
Inside each loop RowCreatedDate  
Inside each loop ID  
In off\_limits\_idCturnover\_currUSDlast\_modification\_date2009/12/01  
14:51:00corp\_status\_idQUOcreated\_user9999999ultimate\_parent\_id2fullnameAbbott  
LaboratoriesU\_Size\_Code1last\_modification\_by40001040turnover22476322created\_datetime1978/01/01  
00:00:00company\_id2 loop  
Inside each loop OffLimitsID  
Inside each loop TurnoverCurrencyTypeID  
Inside each loop RowModifiedDate  
Inside each loop CorporateOwnershipTypeID  
Inside each loop RowCreatedBy  
Inside each loop GlobalUltimateID  
Inside each loop Name  
Inside each loop CorporateSizeTypeID  
Inside each loop RowModifiedBy  
Inside each loop TurnoverAmount  
Inside each loop RowCreatedDate  
Inside each loop ID  
Loaded suite C:/Users/ecucropc/Documents/Visual Studio  
2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest  
Started  
Inside assert\_equal loop for test\_company\_CorporateOwnershipTypeID  
FInside assert\_equal loop for test\_company\_CorporateSizeTypeID  
FInside assert\_equal loop for test\_company\_GlobalUltimateID  
.Inside assert\_equal loop for test\_company\_ID  
.Inside assert\_equal loop for test\_company\_Name  
.Inside assert\_equal loop for test\_company\_OffLimitsID  
FInside assert\_equal loop for test\_company\_RowCreatedBy  
.Inside assert\_equal loop for test\_company\_RowCreatedDate  
.Inside assert\_equal loop for test\_company\_RowModifiedBy  
.Inside assert\_equal loop for test\_company\_RowModifiedDate  
.Inside assert\_equal loop for test\_company\_TurnoverAmount  
.Inside assert\_equal loop for test\_company\_TurnoverCurrencyTypeID  
F  
Finished in 0.029 seconds.

1. Failure:  
test\_company\_CorporateOwnershipTypeID(TestSuite)  
[C:/Users/ecucropc/Documents/Visual Studio  
2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:  
\<"QUO "\> expected but was  
\<1\>.

2. Failure:  
test\_company\_CorporateSizeTypeID(TestSuite)  
[C:/Users/ecucropc/Documents/Visual Studio  
2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:  
 expected but was  
\<1\>.

3. Failure:  
test\_company\_OffLimitsID(TestSuite) [C:/Users/ecucropc/Documents/Visual  
Studio  
2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:  
\<“C”\> expected but was  
.

4. Failure:  
test\_company\_TurnoverCurrencyTypeID(TestSuite)  
[C:/Users/ecucropc/Documents/Visual Studio  
2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:  
\<“USD”\> expected but was  
\<166\>.

12 tests, 12 assertions, 4 failures, 0 errors

If I comment out the ‘define\_method’ block of code, it will continue  
where I was expecting it to go.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [April 13, 2010, 3:00pm UTC](https://www.ruby-forum.com/t/newbie-control-flow-of-tests-using-define-method/197195/2 "2010-04-13T15:00:43Z")

</div>

Chris Cropleu wrote:

> class TestSuite \< Test::Unit::TestCase
> 
> #Build a hash of Source Database columns and target Database columns
> 
> ```
> companyRRAccessToBeacon = Hash.new
> companyRRAccessToBeacon['company_id'] = 'ID'
> cmpanyRRAccessToBeacon['ultimate_parent_id'] = 'GlobalUltimateID'
> companyRRAccessToBeacon['fullname'] = 'Name'
> companyRRAccessToBeacon['turnover_curr'] = 'TurnoverCurrencyTypeID'
> companyRRAccessToBeacon['turnover'] = 'TurnoverAmount'
> companyRRAccessToBeacon['u_size_code'] = 'CorporateSizeTypeID'
> companyRRAccessToBeacon['corp_status_id'] =
> 
> ```
> 
> ‘CorporateOwnershipTypeID’  
> companyRRAccessToBeacon[‘created\_user’] = ‘RowCreatedBy’  
> companyRRAccessToBeacon[‘created\_datetime’] = ‘RowCreatedDate’  
> companyRRAccessToBeacon[‘last\_modification\_by’] = ‘RowModifiedBy’  
> companyRRAccessToBeacon[‘last\_modification\_date’] =  
> ‘RowModifiedDate’  
> companyRRAccessToBeacon[‘off\_limits\_id’] = ‘OffLimitsID’  
> Wowzer. How about this?  
> companyRRAccessToBeacon = {‘company\_id’ =\> ‘ID’, ‘ultimate\_parent\_id’ =\>  
> ‘GlobalUltimateID’ }  
> And so on and so forth ? A little cleaner, a little easier to read,  
> fewer repetitions.
> 
> ```
> #Connect to the RRAccess and Beacon databases
> rraccesshandle = connectrraccessdb
> beaconhandle = connectbeacondb
> 
> ```

[…]

> ```
> #Loop through each record returned in the Data Driven Test set.
> companyrowData.each do | companydata |
> puts "In #{companydata} loop"
> #If no Company records are returned then run the test.
> companydata["company_id"] != ""
> recordtofetch = companydata["company_id"]
> 
> ```

Where is your if statement? Maybe you mean this?  
recordtofetch = companydata[‘company\_id’] unless  
companydata[‘company\_id’].empty?

> 

[…]

> The output of this is as follows:

[…]

> Inside assert\_equal loop for test\_company\_CorporateOwnershipTypeID  
> FInside assert\_equal loop for test\_company\_CorporateSizeTypeID  
> FInside assert\_equal loop for test\_company\_GlobalUltimateID  
> .Inside assert\_equal loop for test\_company\_ID  
> .Inside assert\_equal loop for test\_company\_Name  
> .Inside assert\_equal loop for test\_company\_OffLimitsID  
> FInside assert\_equal loop for test\_company\_RowCreatedBy  
> .Inside assert\_equal loop for test\_company\_RowCreatedDate  
> .Inside assert\_equal loop for test\_company\_RowModifiedBy  
> .Inside assert\_equal loop for test\_company\_RowModifiedDate  
> .Inside assert\_equal loop for test\_company\_TurnoverAmount  
> .Inside assert\_equal loop for test\_company\_TurnoverCurrencyTypeID  
> F  
> Finished in 0.029 seconds.
> 
> 1. Failure:  
> test\_company\_CorporateOwnershipTypeID(TestSuite)  
> [C:/Users/ecucropc/Documents/Visual Studio  
> 2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:  
> \<"QUO "\> expected but was  
> \<1\>.

Well, in your first test, you get a ‘1’ back and you apparently expect a  
"QUO ".  
Anything else the errors can tell you that they can’t tell us?  
Look into this and get back to us a little later… The tests work,  
they’re telling you that they fail 🙂

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [April 14, 2010, 2:42pm UTC](https://www.ruby-forum.com/t/newbie-control-flow-of-tests-using-define-method/197195/3 "2010-04-14T14:42:37Z")

</div>

Aldric G. wrote:

> Chris Cropleu wrote:
> 
> > class TestSuite \< Test::Unit::TestCase
> > 
> > #Build a hash of Source Database columns and target Database columns
> > 
> > ```
> > companyRRAccessToBeacon = Hash.new
> > companyRRAccessToBeacon['company_id'] = 'ID'
> > cmpanyRRAccessToBeacon['ultimate_parent_id'] = 'GlobalUltimateID'
> > companyRRAccessToBeacon['fullname'] = 'Name'
> > companyRRAccessToBeacon['turnover_curr'] = 'TurnoverCurrencyTypeID'
> > companyRRAccessToBeacon['turnover'] = 'TurnoverAmount'
> > companyRRAccessToBeacon['u_size_code'] = 'CorporateSizeTypeID'
> > companyRRAccessToBeacon['corp_status_id'] =
> > 
> > ```
> > 
> > ‘CorporateOwnershipTypeID’  
> > companyRRAccessToBeacon[‘created\_user’] = ‘RowCreatedBy’  
> > companyRRAccessToBeacon[‘created\_datetime’] = ‘RowCreatedDate’  
> > companyRRAccessToBeacon[‘last\_modification\_by’] = ‘RowModifiedBy’  
> > companyRRAccessToBeacon[‘last\_modification\_date’] =  
> > ‘RowModifiedDate’  
> > companyRRAccessToBeacon[‘off\_limits\_id’] = ‘OffLimitsID’  
> > Wowzer. How about this?  
> > companyRRAccessToBeacon = {‘company\_id’ =\> ‘ID’, ‘ultimate\_parent\_id’ =\>  
> > ‘GlobalUltimateID’ }  
> > And so on and so forth ? A little cleaner, a little easier to read,  
> > fewer repetitions.
> > 
> > ```
> > #Connect to the RRAccess and Beacon databases
> > rraccesshandle = connectrraccessdb
> > beaconhandle = connectbeacondb
> > 
> > ```
> 
> […]
> 
> > ```
> > #Loop through each record returned in the Data Driven Test set.
> > companyrowData.each do | companydata |
> > puts "In #{companydata} loop"
> > #If no Company records are returned then run the test.
> > companydata["company_id"] != ""
> > recordtofetch = companydata["company_id"]
> > 
> > ```
> 
> Where is your if statement? Maybe you mean this?  
> recordtofetch = companydata[‘company\_id’] unless  
> companydata[‘company\_id’].empty?
> 
> > 
> 
> […]
> 
> > The output of this is as follows:
> 
> […]
> 
> > Inside assert\_equal loop for test\_company\_CorporateOwnershipTypeID  
> > FInside assert\_equal loop for test\_company\_CorporateSizeTypeID  
> > FInside assert\_equal loop for test\_company\_GlobalUltimateID  
> > .Inside assert\_equal loop for test\_company\_ID  
> > .Inside assert\_equal loop for test\_company\_Name  
> > .Inside assert\_equal loop for test\_company\_OffLimitsID  
> > FInside assert\_equal loop for test\_company\_RowCreatedBy  
> > .Inside assert\_equal loop for test\_company\_RowCreatedDate  
> > .Inside assert\_equal loop for test\_company\_RowModifiedBy  
> > .Inside assert\_equal loop for test\_company\_RowModifiedDate  
> > .Inside assert\_equal loop for test\_company\_TurnoverAmount  
> > .Inside assert\_equal loop for test\_company\_TurnoverCurrencyTypeID  
> > F  
> > Finished in 0.029 seconds.
> > 
> > 1. Failure:  
> > test\_company\_CorporateOwnershipTypeID(TestSuite)  
> > [C:/Users/ecucropc/Documents/Visual Studio  
> > 2008/Projects/DataSynchronisation/DataSynchronisation/CommonTest.rb:57]:  
> > \<"QUO "\> expected but was  
> > \<1\>.
> 
> Well, in your first test, you get a ‘1’ back and you apparently expect a  
> "QUO ".  
> Anything else the errors can tell you that they can’t tell us?  
> Look into this and get back to us a little later… The tests work,  
> they’re telling you that they fail 🙂

Thanks for your feedback. You are quite right, the assertion is giving  
me the failures I was expecting, however my rather large code snippet  
probably did not highlight my particular problem in the loop control I  
was trying to describe.

I have since spoken with some wiser heads (namely the developers on the  
team) and they suggested a good method for me to move forward on this.

I have now captured the column differences (if there are any) in a hash  
and the put that hash into an assert\_nil test.

Much cleaner and I only need to run one test as opposed to the potential  
hundreds in my original solution.

Regards,

Chris
