How to set scenario in unit testing?

Hello,if my rules like it,how to set scenario in the unit testing? thanks!

rules:

array(‘username, password’, ‘required’, ‘on’=>‘register’)

Use $model->setScenario(‘register’)? or $model->scenario=‘register’?

I try it,but not succeed.

Hi,

Do u have actionregister method ?

That really is the way to set a scenario unless you’re using the constructor right away. Can you tell us what you mean by “[does] not succeed?”

if my rules like this:

array(‘username, password’, ‘required’, ‘on’=>‘register’)

and my function in unit testing like this:

public function testCreate()

{

$newUser=new User;

[color="#FF0000"]$newUser->setScenario(‘register’);[/color]

$newUser->setAttributes(array(

'username'=>'TestUserOne',


'password'=>'12345',


));

$this->assertEquals(true, $newUser->save());

}

It will show:

Tests: 1, Assertions: 1, Failures: 1.

I want to know,how to set validate scenario in unit testing? where is wrong in my code?

Nothing is wrong with your code. There can be many reasons why save() fails. Check all other validation rules, the return value of User::beforeSave() and all attached behaviors (if any).

Could be it’s “just” the saving operation failing. Try to check [font=“Courier New”]$this->assertTrue($newUser->validate())[/font] first.

Thank you very much! in "register" scenario, the "password_repeat" need to verify:

$newUser=new User;

$newUser->setScenario(‘register’);

$newUser->setAttributes(array(

‘username’=>‘TestUserOne’,

‘password’=>‘12345’,

‘password_repeat’=>‘12345’,

));