laravel - Is there a better way to write this test? -


i'm new unit testing laravel , mockery , wrote following test. passes , seems work. however, think it's written in better way. seems test more break implementation. there better ways this?

itemmodeltest.php

... public function mock($class) {     $mock = mockery::mock($class);      $this->app->instance($class, $mock);      return $mock; }  // seems test might more break method itself. public function testcattree() {     $categorymock = $this->mock("category");     $categorymock->shouldreceive("getattribute")->with("name")->times(3)         ->andreturn("self","parent1","parent2");      $categorymock->shouldreceive("getattribute")->with("parent")->times(3)         ->andreturn($categorymock, $categorymock, null);      $i = new item;     $i->setattribute("category",$categorymock);     $tree = $i->cattree;     should::equal("parent2 > parent1 > self", $tree); } 

item.php

class item extends eloquent {     public function category() {         return $this->belongsto("category");     }      public function getcattreeattribute() {         $category = $this->category;         if(!$category) return "";         $tree = array($category->name);         $parent = $category->parent;         while($parent) {             $tree[] = $parent->name;             $parent = $parent->parent;         }         return implode(" > ", array_reverse($tree));     } } 

category.php*

class category extends eloquent {      public function getparentattribute() {         return $this->belongsto('category','parent_id');     }  } 

i dont think bad.

the test should never "break" - because if does, means method broke first. whole reason have tests.

if need change method, write new test first (i.e. test driven development).

you might consider reading jeffery way ebook on testing laravel 4 - worth every cent.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -