PhpSpec。当mock必须返回一个对象时,调用非对象的成员函数

时间:2015-11-22 12:43:49

标签: php symfony bdd phpspec

我是新的phpspec,并试图创建虚拟转换器用于学习目的,但我坚持调用非目标错误的成员函数

这是我的代码:

Conver类

private $supportedFormats = ['xml', 'json'];
private $handler;

public function __construct(ConvertHandler $handler)
{
    $this->handler = $handler;
}

public function convert(array $data, $format)
{
    if (!$this->shlouldBeSupportedFormat($format)) {
        throw new \Exception();
    }
    $converter = $this->handler->getConverter($format);

    return $converter->convert($data);
}

public function shlouldBeSupportedFormat($format)
{
    if (!in_array($format, $this->supportedFormats)) {
        return false;
    }

    return true;
}

ConvertHandler类

public function getConverter($format)
{
    $class = "AppBundle\Service\Converter\\".ucfirst($format).'Converter';
    if (class_exists($class)) {
        return new $class();
    } else {
        throw new \Exception();
    }
}

XmlConverter类

public function convert(array $data)
{
    return str_replace(PHP_EOL, '', TypeConverter::toXml($data));
}

此代码正常运行。它以XML格式返回字符串。

现在测试:

ConvertSpec类

function let(ConvertHandler $handler) {
    $this->beConstructedWith($handler);
}

function its_convert_function_should_be_valid()
{
    $this->shouldThrow('Exception')->during('convert', ['sadsa','xml']);
    $this->shouldThrow('Exception')->during('convert', [[],'bad_format']);
}

function it_should_check_if_support()
{
    $this->shlouldBeSupportedFormat('xml')->shouldReturn(true);
    $this->shlouldBeSupportedFormat('bad_format')->shouldReturn(false);
}

function it_should_covert_to_xml(ConvertHandler $handler)
{
    $this->convert(['test' => 'test'], 'xml')->shouldBeString();
    $handler->getConverter('xml')->shouldBeCalled();
}

ConvertHandlerSpec类

function its_get_converter_shouldBe_valid()
{
    $this->shouldThrow('Exception')->during('getConverter', ['wrong_format']);
}

function it_should_return_converter()
{
    $this->getConverter('xml')->shouldReturnAnInstanceOf('AppBundle\Service\Converter\XmlConverter');
}

XmlConverterSpec类

function it_should_implement()
{
    $this->shouldImplement('AppBundle\Service\Converter\ConverterInterface');
}
function it_should_convert()
{
    $this->convert(['test'=>'test'])->shouldBeString();
    $this->convert(['test'=>'test'])->shouldReturn(
        '<?xml version="1.0" encoding="utf-8"?><root><test>test</test></root>'
        );
}

运行测试时出错: PHP致命错误:在第25行的/var/www/phpSpecTest/src/AppBundle/Service/Convert.php中调用非对象的成员函数convert()

我已经尝试第二天修复它,并在网上寻找解决方案。但仍无法找到解决方案。任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:4)

您尚未在>>> df col1 col2 col3 0 1 10 100 1 2 20 200 2 3 30 300 3 4 40 400 4 5 50 500 5 6 60 600 >>> arr2D = np.column_stack((df['col1'],df['col2'],df['col1'],df['col3'])) >>> out_list = np.vsplit(arr2D.reshape(-1,2),arr2D.shape[0]) >>> print out_list [array([[ 1, 10], [ 1, 100]]), array([[ 2, 20], [ 2, 200]]), array([[ 3, 30], [ 3, 300]]), array([[ 4, 40], [ 4, 400]]), array([[ 5, 50], [ 5, 500]]), array([[ 6, 60], [ 6, 600]])] 规范方法中模仿ConverterHandler->getConverter()方法。应该是这样的:

let