Yii Framework Forum: phpunit: Failed asserting that two strings are equal. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

phpunit: Failed asserting that two strings are equal.

#1 User is offline   yii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 25-July 09

Posted 10 October 2010 - 01:55 PM

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.


Meine Fragen stelle ich öffentlich und baue somit eine Wissensdatenbank für andere auf!
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,134
  • Joined: 17-January 09
  • Location:Russia

Posted 11 October 2010 - 05:09 AM

It's not strange. It means that what you are comparing are not equal.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   digitalzombie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 19-August 10

Posted 15 October 2010 - 11:03 AM

View Postyii, on 10 October 2010 - 01:55 PM, said:

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.





I got the same problem and I figured it out.

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

#4 User is offline   ale3584 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-December 10

Posted 28 December 2010 - 05:04 AM

I have the same problem.
0

#5 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 28 December 2010 - 05:12 AM

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
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#6 User is offline   Khad 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-May 11

Posted 20 May 2011 - 06:23 AM

Same problem here. Any fix ?

I've been following the book but I keep getting errors all the way through it...
0

#7 User is offline   jefftulsa 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 168
  • Joined: 06-October 08
  • Location:Austin, TX

Posted 20 May 2011 - 09:55 PM

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();
	}
	
}

0

#8 User is offline   Khad 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-May 11

Posted 21 May 2011 - 04:30 AM

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
0

#9 User is offline   iansane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 11

Posted 08 November 2011 - 04:58 PM

View PostKhad, on 21 May 2011 - 04:30 AM, said:

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

#10 User is offline   elenalee 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 11-November 11

Posted 11 November 2011 - 01:33 PM

good
0

#11 User is offline   hnam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 10
  • Location:BSP

Posted 20 December 2011 - 06:29 AM

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
$newProject=new Project;
$newProjectName = 'Test Project Creation';
$newProject->setAttributes(array(
'name' => $newProjectName,
'description' => 'This is a test for new project creation',
)
);
//set the application user id to the first user in our users fixture data
Yii::app()->user->setId($this->users('user1')->id);
//save the new project, triggering attribute validation
$this->assertTrue($newProject->save());

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

$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);
}
On revolutions !
0

#12 User is offline   elruinnou 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-May 12

Posted 29 May 2012 - 01:11 AM

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.. :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users