phpunit: Failed asserting that two strings are equal.


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';


		'name'			=> $newProjectName,

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






	$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.


Time: 3 seconds, Memory: 8.25Mb

There was 1 failure:

1) ProjectTest::testCreate

Failed asserting that two strings are equal.

--- Expected

+++ Actual

@@ @@





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:



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


class Project extends [b]TrackStarActiveRecord[/b]




abstract class TrackStarActiveRecord extends CActiveRecord



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


	protected function beforeValidate()




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

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





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

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




		return parent::beforeValidate();





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


must become :

//save the new project, triggering attribute validation


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



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.


I am using Yii 1.1.8

and getting unit Testing issue :-


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'; 


  '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


  //save the new project, triggering attribute validation


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



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


$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… :)