【问题标题】:Testing and Mocking. Who's talkin?测试和模拟。谁在说话?
【发布时间】:2015-01-27 23:35:02
【问题描述】:

这是我的测试

 use Lean\Core;
 use Symfony\Component\HttpFoundation\Request;

class TestCore extends PHPUnit_Framework_TestCase{
    public function tearDown() {
        Mockery::close();
    }
    public function test_handle_returns_a_response(){
        $request = Mockery::mock("Request");
        $request->shouldReceive("create")->once()->andReturn(new Request);
        $core = new Core();
        $response = $core->handle($request->create("d"));
        $output = $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response',$response);
    }
} 

这是我的核心类的句柄函数

 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
    {
        switch($request->getPathInfo()) {
            case "/":
                return new Response("Welcome to the main page");
                break;
            case "/d":
                return new Response("d");
                break;
            default:
                return new Response("This route doesn't exist");
        }

我觉得我在做一些非常愚蠢的事情。 我使用 PHP 5.6 和 Mockery + PHPUNIT。 这可行,但是当我尝试返回模拟对象时,句柄函数说它需要一个 Request 实例

更新

只是为了澄清,这是有效的。 但我基本上是反直觉的。我希望能够通过模拟本身

【问题讨论】:

    标签: php testing phpunit mockery


    【解决方案1】:

    我认为您需要提供请求的全名。

    $request = Mockery::mock("Symfony\Component\HttpFoundation\Request");
    

    【讨论】:

    • 哦,谢谢。这行得通。我只需要了解它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2022-01-21
    • 2019-03-26
    • 2017-03-27
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多