Codeception Testing with Yii Framework 2.0 [Day 5]

Today’s Objective(s)

  1. Update testModelArray() unit test to confirm that returned object is an array with correct values
  2. Explore and understand the basic functions of basic test methods (Understand what is going on in testModelBoolean() function to be precise).
  3. Try to use the entire $I->wantTo(‘load all values’); style of Codeception

Today’s Method

So today we have a list of objectives, there are 2 reasons for this:

  1. I’d like to keep track of the objectives set out from yesterday (Simple copy/paste)
  2. If I finish the top objective for the day with time to spare I can move on to the next objective without too much hassle.

And will keep the basic method from yesterday with actions in headings followed by the meat of it.

Actions

Update Test to use correct function

Here’s what we’re working with:

 $model = new MySimpleModel();

 $this->specify('comma seperated string should return array of items', function () use ($model) {
   expect('passing "dave,joe" should return array with ["dave","joe"]', $model->array("dave,joe"))->array(['dave','joe']);
   expect('execution with no argument should return empty array', $model->array())->array();
 });

Altered to use the correct function in MySimpleModel Class:

 $model = new MySimpleModel();

 $this->specify('comma seperated string should return array of items', function () use ($model) {
   expect('passing "dave,joe" should return array with ["dave","joe"]', $model->getArray("dave,joe"))->array(['dave','joe']);
   expect('execution with no argument should return empty array', $model->getArray())->array();
 });

And after that alteration, test still fails thanks to my assumption on the array assertion. On to the next step :)

Update Test to use correct assertion for array and values

Modified the test to this:

 $model = new MySimpleModel();

 $this->specify('comma seperated string should return array of items', function () use ($model) {
   $result = $model->getArray("dave,joe");
   $this->assertInternalType('array', $result);
   $this->assertEquals(['dave','joe'], $result);
 });
 
 $this->specify('no parameter passed should return empty array', function () use ($model) {
   $result = $model->getArray();
   $this->assertInternalType('array', $result);
   $this->assertEquals([false], $result);
 });

And the test completes without issue. I have a sneaking suspicion that there is a better way to do those tests, but this gets the job done and I’ll keep improving as I go.

Conclusion

With unit tests passing I must admit that I quite like this process of development. It might add a bit more time, but it’s an excellent method of documenting methods and once the work has been done it can be used to keep piece of mind when changes have been made.

Upcoming Objective(s)

  1. Update testModelArray() unit test to confirm that returned object is an array with correct values
  2. Explore and understand the basic functions of basic test methods (Understand what is going on in testModelBoolean() function to be precise).
  3. Try to use the entire $I->wantTo(‘load all values’); style of Codeception

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>