Unit Testing - creating new records

I wrote a test case to create a new record but it doesn’t create the record in mysql database.

whereas I wrote test case to delete a record which actually deletes the record in database. What could be the reason?

Below is the test case for create and delete:




class EmployeeTest extends CDbTestCase

{

	/**

	 * We use 'Employee' fixture.

	 * @see CWebTestCase::fixtures

	 */

	public $fixtures=array(

		'employees'	=>	'Employee',

	);


	public function testCreate()

	{

		$employee=new Employee;

		$employee->setAttributes(array(

			'username'	=>	'testcreate',

			'password'	=>	'testcreate',

			'email'		=>	'create@test.com',

		),false);

		$this->assertTrue($employee->save(false)); // does not save in DB but returns true

		$this->assertEquals(Employee::model()->findByPk(4)->id,$employee->id); // returns true

	}

	

	public function testDelete()

	{

		$employee=new Employee;

		$this->assertTrue($employee->findByPk(2)->delete(false)); // deletes in DB and returns true

	}

	

}



It does. For sure :)

You can’t see the new record in the database after running tests, because CDbTestCase reinitializes the database table before each test method. Comment out testDelete() method in your test class, then check the result.