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:
creating (or recreating) mocked class in each test
creating single instance of mocked class property of test class (initializing in constructor or in
setupmethod) - hope here using single instance each test solve problem.changing
call()method in testsaction()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
Post a Comment