dongsui8162 2015-03-18 20:28
浏览 39
已采纳

阻止PHPUnit的DBUnit在数据库中插入XML数据集

Is there a way to stop DBUnit from creating a record based on an XML dataset?

I'm trying to test if a query will insert a record then compare it to the XML dataset.

public function testInsert()
{
    $statement = $this->getConnection()
        ->getConnection()
        ->query(
        'insert into Grades (Vendor) values (\'test\')'
    );

    $actual = $this->getConnection()->createQueryTable(
        'Grades',
        'select Vendor from Grades'
    );

    $this->assertTablesEqual(
        $this->getDataSet()->getTable('Grades'),
        $actual
    );
}

public function getDataset()
{
    //returns dataset below
}

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
    <Grades
        uid="345"
    />
</dataset>

The result is:

PHPUnit: Failed asserting that 

+----------------------+
| Grades               |
+----------------------+
|        Vendor        |
+----------------------+
|         test         |
+----------------------+
|         test         |
+----------------------+

 is equal to expected 
+----------------------+
| Grades               |
+----------------------+
|        Vendor        |
+----------------------+
|         test         |
+----------------------+

Can someone explain why the XML Dataset is inserted at all and how I can stop it from doing so automatically?

  • 写回答

1条回答 默认 最新

  • duanji7881 2015-03-19 14:24
    关注

    Found out I'm supposed to use an empty "base" table XML before comparing results.

    public function getDataSet()
    {
        return $this->createFlatXMLDataSet("seed.xml");
    }
    
    public function testUpdate()
    {
        $statement = $this->getConnection()
            ->getConnection()
            ->query(
            'insert into Grades (Vendor) values (\'test\')'
        );
    
        $actual = new \PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
    
        $actual->addTable(
            'Grades',
            'select Vendor from Grades'
        );
    
        $expected = $this->createFlatXMLDataSet("expected.xml");
    
        $this->assertDataSetsEqual(
            $expected,
            $actual
        );
    }
    

    seed.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <dataset>
        <Grades
        />
    </dataset>
    

    expected.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <dataset>
        <Grades
            Vendor="test"
        />
    </dataset>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?