phpunit: Failed asserting that two strings are equal.

Hi,

I’ve got an strange error if I’m comparing the user id.

(Page 153)




public function testCreate()

{

	$newProject = new Project;

	$newProjectName = 'Test Project Creation';

	$newProject->setAttributes(array(

		'name'			=> $newProjectName,

		'description'	=> 'This is a test for new project creation',

	));

		

	Yii::app()->user->setId($this->users('user1')->id);

	$this->assertTrue($newProject->save());

		

	$retrievedProject = Project::model()->findByPk($newProject->id);

	$this->assertTrue($retrievedProject instanceof Project);

	$this->assertEquals($newProjectName, $retrievedProject->name);

		

	$this->assertEquals(Yii::app()->user->id, $retrievedProject->create_user_id); //LINE 33

}






root@xme-laptop:/var/www/trackstar/protected/tests# phpunit unit/ProjectTest.php

PHPUnit 3.4.15 by Sebastian Bergmann.


.F.


Time: 3 seconds, Memory: 8.25Mb


There was 1 failure:


1) ProjectTest::testCreate

Failed asserting that two strings are equal.

--- Expected

+++ Actual

@@ @@

-1

+


/var/www/trackstar/protected/tests/unit/ProjectTest.php:33


FAILURES!

Tests: 3, Assertions: 6, Failures: 1.




It’s not strange. It means that what you are comparing are not equal.

I got the same problem and I figured it out.

It’s probably because you didn’t extend the class correctly to the TrackstarActiveRecord.

I have the same problem.

Why it should be equal?

Yii::app()->user->id, $retrievedProject->create_user_id

You retrieve the already created project but none of the lines above set its create_user_id to that of Yii::app()->user->id

Same problem here. Any fix ?

I’ve been following the book but I keep getting errors all the way through it…

Please read DigitalZombie’s reply for what looks to be the answer. You must ensure that the Project class extends the base active record class TrackStarActiveRecord. It is in this base class where the create_user_id is being set to the current app user id, and indeed part of what is being tested:


<?php


/**

 * This is the model class for table "tbl_project".

 */

class Project extends [b]TrackStarActiveRecord[/b]

{

 	...




<?php

abstract class TrackStarActiveRecord extends CActiveRecord

{

	 /**

	 * Prepares create_time, create_user_id, update_time and update_user_id attributes before performing validation.

	 */

	protected function beforeValidate()

	{

		if($this->isNewRecord)

		{

			// set the create date, last updated date and the user doing the creating  

			$this->create_time=$this->update_time=new CDbExpression('NOW()');

        	$this->create_user_id=$this->update_user_id=Yii::app()->user->id;

    	}

		else

		{

			//not a new record, so just set the last updated time and last updated user id 	

			$this->update_time=new CDbExpression('NOW()');

			$this->update_user_id=Yii::app()->user->id;

		}

		

		return parent::beforeValidate();

	}

	

}

Hello,

I’ve just found the solution, maybe it will be useful to someone else so I’m posting it. The issue was not my Project class, it was my ProjectTest class (function testCreate()) :




//save the new project, triggering attribute validation

$this->assertTrue($newProject->save(false));



must become :




//save the new project, triggering attribute validation

$this->assertTrue($newProject->save());



It was specified in the book (in bold page 151) so it’s my mistake, it seems I can’t follow correctly :)

Regards,

Khad

Thanks for posting this. I also missed it. That one small word ‘false’ is easy to miss. Most of the issues I’ve had through chapter 8 so far have been tiny mistakes such as this so we have to go back and carefully scan every line altered. Not so fun if you take big steps and have to go way back.

good

I am using Yii 1.1.8

and getting unit Testing issue :-

ProjectTest::testCreate

Failed asserting that null matches expected ‘1’

I have checked my code perfect as per given book and dually checked as per "khad" comments on this forum i dont have this silly mistakes. My code is :

public function testCreate()

{ 


  //CREATE a new Project 


  &#036;newProject=new Project; 


  &#036;newProjectName = 'Test Project Creation'; 


  &#036;newProject-&gt;setAttributes(array( 


  'name' =&gt; &#036;newProjectName, 


  'description' =&gt; 'This is a test for new project creation', 


   )


);	


//set the application user id to the first user in our users fixture data


  Yii::app()-&gt;user-&gt;setId(&#036;this-&gt;users('user1')-&gt;id);


  //save the new project, triggering attribute validation


  &#036;this-&gt;assertTrue(&#036;newProject-&gt;save());





  //READ back the newly created Project to ensure the creation worked


 


  &#036;retrievedProject=Project::model()-&gt;findByPk(&#036;newProject-&gt;id);


  &#036;this-&gt;assertTrue(&#036;retrievedProject instanceof Project);


  &#036;this-&gt;assertEquals(&#036;newProjectName,&#036;retrievedProject-&gt;name);

$this->assertEquals(Yii::app()->user->id, $retrievedProject->create_user_id);

}

i got the same problem, but i fix this by make sure all the variables name, cause the code was not same at all, for example: in the testUpdate there is, $updatedProject = Project::model()->findByPk($project->id);

but in the previous code that’s written, $updatedProject = Project::model()->findByPk($newProject->id);

hope it helps somebody… :)