在Symfony2中运行PHPUnit测试时,JMS Serializer无法读取配置

ben*_*ole 4 serialization phpunit serializer symfony jmsserializerbundle

我正在使用JMSSerializer为我正在研究的S​​ymfony2项目创建JSON响应,并尝试为每个响应构建单元测试,但我正在尝试以下变体:

JMS\Serializer\Exception\RuntimeException: You must define a type for FooBundle\Entity\Bar::$name.
Run Code Online (Sandbox Code Playgroud)

我正在为序列化器使用YML配置,它在生成响应时非常有效.

#src/FooBundle/Resources/config/serializer/Entity.Bar.yml
FooBundle\Entity\Bar:
  exclusion_policy: none
    properties:
        id:
            exclude: true
            type: integer
        name:
            type: string
Run Code Online (Sandbox Code Playgroud)

我想知道是否需要以某种方式预加载配置并找到此链接:http: //jmsyst.com/libs/serializer/master/configuration,它说配置元数据路径但也包含文件后缀:

$serializer =
JMS\Serializer\SerializerBuilder::create()
    ->addMetadataDir($someDir)
    ->build();
Run Code Online (Sandbox Code Playgroud)

我在单元测试中生成序列化器时尝试设置config目录:

$serializer = SerializerBuilder::create()->addMetadataDir('path-to-dir')->build();
Run Code Online (Sandbox Code Playgroud)

但这并没有解决问题,我再次检查了文档页面,它告诉您列出文件的完整路径

"因此,如果您的类名为Vendor\Package\Foo,则元数据文件需要位于$ someDir/Vendor.Package.Foo.(xml | yml)."

但尝试得到生成:

JMS\Serializer\Exception\InvalidArgumentException: The directory "path-to-file" does not exist.
Run Code Online (Sandbox Code Playgroud)

我错过了一些明显的东西吗

谢谢

K. *_*ert 7

JMSSerializer在第一次读取配置文件时对其进行缓存,您必须使用以下命令清除测试环境的缓存:

php app/console cache:clear -e test
Run Code Online (Sandbox Code Playgroud)