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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改