Unit testing with logged in user

Hi there!

Which is the best practice to unit test with a logged in user? I do it this way:

class TestUserIdentity extends CUserIdentity

  public function authenticate() {


    return $this->errorCode=self::ERROR_NONE;



class TestUserIdentityTest extends CTestCase {

  function testLogin() {

    $identity = new TestUserIdentity('testuser','');



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



This way, if I need a logged in user, I can do a @depends testLogin on the unit test. But is this the best way?

I write this:

$_identity=new UserIdentity($login,$password);

$return = $_identity->authenticate();


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