Problems with unitesting


I don’t know if this is a bug, but when I try to test (phpunit test) a model with a renderPartial it doesn’t work

Even this simple test - NO TEST - doesn’t work

public function testSimple(){

  Yii::app()->controller->renderPartial(PATH TO A VIEW FILE, param(),TRUE);


The output is the following:

Fatal error: Call to a member function renderPartial() in THE FILE SCRIPT

Well, the test doesn’t work but the model works perfect.

Somebody knows How to test a model that includes a render partial function?


You are testing model without application context so you don’t have controller there. To test your code as a whole you can look at functional testing.

Hi samdark .

I’m using netbeans, I have several test, and they work ok.

If I test my db cx it works, even If I use the Yii:app() objet

public function testConnection(){


        $this->assertNotEquals(NULL, $cx);


A functional testing is not the solution in my case because I’m testing a model with a function that sends an email, there is where I use the partial render for the email body.

Some clues?


I meant you, of course, have an application instantiated but it’s done via bootstrap script: you are not processing web request so no controller is being used and you can’t get it from your Model.


what if you put the email sending part itself into the Controller instead of the Model?


thanks man, These are my first steps in unit testing - yii is amazing!! I come from using CodeIgniter and I feel yii like a F16 full army

Thanks imehesz, I think I should move my sending email code from the model. So, to have a unit test for the sending email function I should use a funtional test?

Thanks men

Not necessary. You can move sending emails to Email component and test it separated from controller and model.