TDD Testing models with unit test


(juanborja) #1

Hi! I m Juan from Argentina. Im started to work with yii2 like a year hago. Im usually read the forum, but this its my first question. I want to ask about tdd in yii2, i read a lot about this in general and in yii en particular. Im using codeception and started from the unit tests. I made a couple of tests with some work, and everything run very well. But i cant find deeper information about testing yii2 or mvc pattern in general. I glade to know about real cases of use, becouse all the examples that i can find meke a really simple test. I like to know:

All the questions are about models (this its all i tryed)

  1. How can i know that my test is valid? it is enough set the model with invalid data and and valid data with the correspondent assertions?
  2. Should i test the models with any conecction to de database in first place?
  3. How can i use gii? if tdd says that i should only write the minimun code to pas the test?
  4. It have any sense write test for models generated with gii?
  5. How i supouse to mock the realition between models?

i would like to see some real example of unit test in yii

Thanks

Juan


(Alexander Makarov) #2
  1. Depends on what your model is doing. I’d test ability to save with scenarios you use and custom validators. If there’s more custom methods, test them. Focusing on active record out of the box functionality doesn’t make much sense since it’s covered by framework tests.
  2. Depends on what do you want to assert.
  3. How’s that connected?
  4. Not before you customize them.

(juanborja) #3

Thanks for the quick answer. I’m realized that my third question is not clear. Im try to say that the gii generate more code than the necessary to pass the test, but may be is not a big thing.

Thanks again!