testing - ZF2 测试 - serviceManager 忽略的模拟对象

标签 testing zend-framework2 mocking phpunit

我正在尝试在 Zend Framework 2.3 中的 Controller 上运行测试。

我测试的 Action 是这样的:

public function listAction() {

        // optional filtering
        $filter = $this->params('filter'); // filter type

        // params from JSON
        $jsonPost = $this->getRequest()->getContent();
        $params = json_decode($jsonPost);
        $param1 = isset($params->query1) ? $params->query1 : NULL;
        $param2 = isset($params->query2) ? $params->query2 : NULL;

        $json = Array( // json container
            'status' => TRUE,
            'data' => Array(),
            ); 

        try {

            // get data from Model
            $list = new Mapping\Books\Listing();

            if( !empty($filter)) { // optional filtering
                $list->addFilter($filter, $param1, $param2);
            }

            $data = $list->setOrder()->getList();

            // final data mapping to JSON and formating
            foreach($data as $isbn => $book) {
                $json['data'][$isbn] = Array(
                    'ISBN' => $book->getISBN(),
                    'title' => $book->getTitle(),
                    'rating' => $book->getRating(),
                    'release_date' => $book->getReleaseDate()->format('F Y'),
                    'authors' => Array() // multiple
                );

                // final authors mapping
                foreach($book->getAuthors() as $author) {
                    $json['data'][$isbn]['authors'][] = Array(
                        'author_id' => $author->getId(),
                        'author_name' => $author->getName()
                    );
                }
            }

        } catch(Exception $e) {
            $json = Array(
                'status' => FALSE,
                'error' => $e->getMessage()
            );        
        }

        return new JsonModel( $json );

    }

我的测试方法是这样的:

public function testListActionWithoutParams()
    {

        // object mocking
        $listingMock = $this->getMockBuilder('Books\Model\Mapping\Books\Listing')
                        ->disableOriginalConstructor()
                        ->setMethods(Array('getData', 'setOrder'))
                        ->getMock();


        $listingMock->expects($this->once())->method('setOrder')
                    ->will($this->returnSelf());

        $listingMock->expects($this->once())->method('getData')
                    ->willReturn( Array() ); // for testing is enough

        $serviceManager = $this->getApplicationServiceLocator();
        $serviceManager->setAllowOverride(true);
        $serviceManager->setService('Books\Model\Mapping\Books\Listing', $listingMock);

        // dispatch
        $this->dispatch('/books');

        // routing tests
        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Books');
        $this->assertControllerName('Books\Controller\Index');
        $this->assertControllerClass('IndexController');
        $this->assertActionName('list');
        $this->assertMatchedRouteName('list');

        // header tests
        $this->assertResponseHeaderContains('Content-type', 'application/json; charset=utf-8');

}

我想我在这里误解了一些东西。我的理解是,serviceManager 也会“通知”自动加载器,实际调用的类应该替换为模拟对象,但它没有。

请问如何用 mocked 替换我的对象?

最佳答案

首先你的 Controller 有很多逻辑,哪里不对。

其次,我只能给你代码的工作流程:

在您的 Controller 中,您需要一个将返回列表对象的方法,因此符合:

// get data from Model
$list = new Mapping\Books\Listing();

应该是新的方法调用:

// get data from Model
$list = $this->getListing();

方法:

public function getListing()
{
    return new Mapping\Books\Listing();
}

现在您需要在 Controller 中模拟此方法。模拟方法返回的对象应该是您模拟的 Mapping\Books\Listing 对象。

关于testing - ZF2 测试 - serviceManager 忽略的模拟对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28529337/

相关文章:

testing - 从命令行运行时,运行 Mocha 测试(记者 html)的 Backbone Js 挂起

php - Zend Framework 2 - 注释形式 - 绑定(bind)不起作用

php - Zend Framework 2 - 数据库连接

vb.net - 在 VB.Net 中使用最小起订量的经验

java - Junit 4 全局设置和拆卸

java - 无法加载 TestContextBootstrapper [null]

visual-studio-2008 - 如何使用 Visual Studio 2008 Team Suite 进行 Web 测试

zend-framework2 - 图像调整大小 zf2

java - jmockit 捕获模拟方法参数

java - 想要但没有调用mockito测试