如果使用 ORM 层,如何在 PHP 中进行单元测试?
我有一堂课,比如说“人”。 ORM层根据sql结构生成了相应的对象。 person 类有一个方法:Get($id)。在 Get 方法中,调用对象 Person 并检索表中的字段。
我基本上想做以下单元测试:创建一个新人并检查 Get 方法是否返回正确的信息。
在看到您的回复后,我也在想,在不实际构建新数据库的情况下模拟 ORM 响应是否不是正确的方法?
I have a class let's say Person. The ORM layer has generated based on the sql structure the corresponding objects. The class person has a method: Get($id). In the Get method, the object Person is called and the field from the table are retrieved.
I basically want to make the following unit test: create a new person and check if the Get method is returning the right information.
I was also wandering after seeing your responses if simulating an ORM response without actually building a new database is not the way to go ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下单元测试应该如何工作?
一般来说,您应该在心理上将单元测试分为两部分:
我需要创建一个单独的数据库吗?
这取决于您的需求。 Rails 应用程序通常具有测试/开发/生产“环境” - 数据库、配置、存储目录。
测试用于运行单元测试,开发用于开发事物,生产用于运行实时服务器。在开发过程中,您将针对开发配置以及开发数据库运行。对于单元测试,使用测试环境,其优点是数据库中的用户不会被删除或破坏。
我喜欢这个概念;在我的 phpunit 测试中,我经常在引导程序中有一个开关来更改加载的配置文件。请记住,您的开发数据库通常包含比单个单元测试所需的数据更多的数据,并且您可能手工制作这些数据并且不想丢失。此外,另一个数据库不需要花钱。
每次测试后我应该清理新数据库吗?
我主要只清理将在测试中使用的桌子。清理数据库可确保您不会因之前的测试而产生副作用。
How is the unit testing supposed to work in this condition ?
Generally, you should split your unittests mentally in two parts:
Do I need to create a separate database
This depends on your needs. Rails apps generally have testing/development/production "environments" - database, configuration, storage directories.
Testing is for running unit tests, dev for developing things and production for running the live server. While developing, you run against the dev configuration and thus your development database. For unit tests, the testing env is used which has the benefit that i.e. users in the database are not deleted or broken.
I like that concept; in my phpunit tests I often do have a switch in the bootstrap that changes which configuration file is loaded. Just remember that your development database often contains more data than a single unit test needs, and you probably hand-crafted that data and do not want to lose. Also, another database does not cost money.
Should I clean the new database each after each test?
I mostly clean the tables only that will be used in the test. Cleaning your database makes sure you don't get side-effects from previous tests.
查看 Phactory。与 PHPUnit 中包含的数据库扩展相比,我更喜欢它,它使将记录插入测试数据库变得非常容易。
您的被测系统 (SUT) 将需要连接到您的测试数据库。这个想法是您只填充正在测试的方法所需的记录。如果测试数据库具有与生产数据库相同的表和字段,则 orm 层应该不重要。
Check out Phactory. I prefer it over the database extensions included in PHPUnit and it makes it really easy to insert records into your test db.
Your System Under Test (SUT) will need to connect to your test database. The idea is that you populate just the records you need for the method you are testing. The orm layer shouldn't matter if the test db has all the same tables and fields as your production database.
PHPUnit 还为此提供了一些帮助,请查看数据库测试。
本质上,您可以编写测试类,以便它们扩展
PHPUnit_Extensions_Database_TestCase
,然后使用getConnection()
和getDataSet()
函数加载数据测试。然后,您可以在 XML 中准确定义要在数据库中测试的内容。
您还可以断言测试生成的 DataSet 等于您的预期:
在本例中,我们仅比较生成的 person 表...因此 person_create_expected.xml 也应该只包含 person 表。
要创建 XML,您可以使用 mysqldump。
PHPUnit also provides some help with this, have a look at Database Testing.
Essentially you can write you Test classes so that they extend
PHPUnit_Extensions_Database_TestCase
and then use thegetConnection()
andgetDataSet()
functions to load up data for the test.Then you can define exactly what you want to test in the database in the XML.
You can also assert that the resulting DataSet from your tests is equal to what you expect with:
In this example, we are only comparing the resulting person table... So person_create_expected.xml should only contain the person table as well.
To create the XML's you can use
mysqldump
.