Why this test fails

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Why this test fails

Post by kpraman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php

   public function testing_update()
   {
      $new=new MemberTypes;
      $new->AddOrUpdateMemberType('manager','Head of the','true','','membertype');
		   
      $membertypeid=mysql_insert_id();
      $new->AddOrUpdateMemberType('commoner','Head  of  ','true',$membertypeid,'membertype');
		   
      $query=mysql_query("SELECT * FROM membertype WHERE membertypeid=$membertypeid");
      $resActual=mysql_fetch_row($query);
		  
      $resExpected=array($membertypeid,'commoner','Head of the','true');
      print_r($resActual);
      print('<br>');
      print_r($resExpected);
      $this->assertEquals($resExpected,$resActual, "UPDATE FAILED");
   }

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

because $resExpected is not the same as $resActual
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

that i know :) . But, if i compare by using array_diff i get empty.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

you don't give much to work with. you posted the test, but not the subject code, or what is being returned, either as the error message or from your print statements. If I had to guess, I would say probably your keys are in a different order between the two arrays?
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

Here it is:

print("Expected");
print_r($resExpected);
print("<br>");
print("Actual");
print_r($resActual);


Output is:

ExpectedArray ( [0] => 98 [1] => commoner [2] => Head of the [3] => true )
ActualArray ( [0] => 98 [1] => commoner [2] => Head of the [3] => true )

if i user assertTrue, the test does not fail
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

and what is the output of the failing test message. Do you perhaps have a trailing space on one of your strings?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

var_dump() is preferable over print_r in these situations. Could you show us what var_dump yields?
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

var_dump output:


actual array(4) { [0]=> string(3) "152" [1]=> string(8) "commoner" [2]=> string(11) "Head of the" [3]=> string(4) "true" }
expected array(4) { [0]=> int(152) [1]=> string(8) "commoner" [2]=> string(11) "Head of the" [3]=> string(4) "true" }
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

kpraman wrote:var_dump output:


actual array(4) { [0]=> string(3) "152" [1]=> string(8) "commoner" [2]=> string(11) "Head of the" [3]=> string(4) "true" }
expected array(4) { [0]=> int(152) [1]=> string(8) "commoner" [2]=> string(11) "Head of the" [3]=> string(4) "true" }

so did you see why it fails....

I would guess changing

Code: Select all

$resExpected=array($membertypeid,'commoner','Head of the','true');

to

$resExpected=array((string)$membertypeid,'commoner','Head of the','true');
should work
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am getting:

Code: Select all

$new->AddOrUpdateMemberType('commoner','Head of the','true',$membertypeid,'membertype');
         $query=mysql_query("SELECT * FROM membertype WHERE membertypeid=$membertypeid");
         $resActual=mysql_fetch_row($query);


         $resExpected=array((string)$membertypeid,'commoner','Head of the','true');



TestCase MembersTypeTest->testing_update() failed: UPDATE FAILED expected Array, actual resActual in D:\Projects\pics4news.com\classes\membertypes_testcase.php:55

Code: Select all

var_dump():

expected array(4) { [0]=> string(3) "555" [1]=> string(8) "commoner" [2]=> string(11) "Head of the" [3]=> string(4) "true" } 
actual array(4) { [0]=> string(3) "555" [1]=> string(8) "commoner" [2]=> string(11) "Head of the" [3]=> string(4) "true" } 

print_r():


expected Array ( [0] => 561 [1] => commoner [2] => Head of the [3] => true ) 
actual Array ( [0] => 561 [1] => commoner [2] => Head of the [3] => true )

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

thanks, its working now.
Post Reply