php - How do I create a partial mock for a controller when I'm calling routes? -


i'm trying test controller method in lumen calling $this->call("get", $route, $data). calls getuserlist method on controller class.

but i'm having problem because getuserlist method calls methods on controller class need mock. attempted set partial mock controller class this:

$controller = mockery::mock("app\http\controllers\mycontroller[mymethod]"); app::instance("app\http\controllers\mycontroller", $controller); 

in each test.

my tests this:

class controllertest extends testcase {     public function test_1()     {         // set stuff          // mock controller         $controller = mockery::mock("app\http\controllers\mycontroller[mymethod]");         app::instance("app\http\controllers\mycontroller", $controller);          // call method         $result = $this->call("get", $route, $data);              // assert stuff     }      public function test_2()     {         // set stuff          // mock controller         $controller = mockery::mock("app\http\controllers\mycontroller[mymethod]");         app::instance("app\http\controllers\mycontroller", $controller);          // call method         $result = $this->call("get", $route, $data);              // assert stuff     } } 

this works fine first test, second , later ones, instance declared forgotten.

i've tried number of different things:

  1. creating (or recreating) mocked class in each test

  2. creating single instance of mocked class property of test class (initializing in constructor or in setup method) - hope here using single instance each test solve problem.

  3. changing call() method in tests action() method (ie. calling controller method directly instead of via http request route).

but none of these approaches worked.

i could rewrite code code needs mocked moved separate library class mocked & used, that's adding layer of complexity. i'm attempting write microservice interfaces particular legacy application's database - needs small number of things don't want add whole lot of code here.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -