如何在使用PHPUnit进行测试时设置Doctrine2灯具?

时间:2022-09-15 19:44:06

I'm trying to get started with Symfony2 and have been trying to set up automated testing for the model layer of my application. The Symfony2 book talks about unit testing for controllers but I can't find many examples of model testing.

我正在尝试使用Symfony2,并一直在尝试为我的应用程序的模型层设置自动化测试。 Symfony2书谈到了控制器的单元测试,但我找不到很多模型测试的例子。

I would like to have a clean data set to work with before each test runs and found these articles:

我希望在每次测试运行之前都有一个干净的数据集,并找到这些文章:

Based on the sznapka.pl article I have a test actually running without errors, but although the test schema is created the fixtures don't load. I can't see why, or even a way to debug this.

根据sznapka.pl文章,我有一个测试实际上没有错误地运行,但是虽然创建了测试模式,但是没有加载。我看不出原因,甚至是一种调试方法。

Background: I've previously worked with CakePHP where the loading of fixtures is largely handled automatically, maybe I have the wrong approach for Symfony/Doctrine?

背景:我以前和CakePHP一起工作过,夹具的加载很大程度上是自动处理的,也许我对Symfony / Doctrine有错误的方法?

3 个解决方案

#1


6  

Yes DoctrineFixtures are a good choice.

是DoctrineFixtures是一个不错的选择。

To test model: you don't really need to load fixtures in the database, you should create objects with the data you want (by injecting it with setters).

要测试模型:您实际上不需要在数据库中加载fixture,您应该使用所需的数据创建对象(通过使用setter注入它)。

To test controller: load doctrine fixtures and use doctrine transactions so the state of your database is the same before each testcase, begin transaction in setUp() and rollback in tearDow(). (If your controller use transactions too i haven't found a good solution yet).

要测试控制器:加载doctrine fixture并使用doctrine事务,这样在每个测试用例之前数据库的状态是相同的,在setUp()中开始事务并在tearDow()中回滚。 (如果您的控制器也使用了交易,我还没有找到一个好的解决方案)。

For fixtures error, if you don't have any error and your fixtures aren't loaded maybe you have missed a naming convention. Can you show us some code ?

对于灯具错误,如果您没有任何错误并且未加载灯具可能您错过了命名约定。你能告诉我们一些代码吗?

#2


4  

Have a look at this solution. I don't think using transactions is the best idea, since chances are that you'll use transactions in your code. This solution suggests to load fixtures manually in each of your test.

看看这个解决方案。我不认为使用事务是最好的主意,因为您可能会在代码中使用事务。此解决方案建议在每次测试中手动加载灯具。

#3


4  

There is a very handy LiipFunctionalTestBundle that simplifies working with a fixtures in test. The basic idea is to create a database each time you run the tests and then load fixtures. Now you can save the models, delete, every test they will be the same.

有一个非常方便的LiipFunctionalTestBundle,可以简化测试中灯具的使用。基本思想是每次运行测试然后加载fixture时创建一个数据库。现在你可以保存模型,删除,每个测试都是一样的。

#1


6  

Yes DoctrineFixtures are a good choice.

是DoctrineFixtures是一个不错的选择。

To test model: you don't really need to load fixtures in the database, you should create objects with the data you want (by injecting it with setters).

要测试模型:您实际上不需要在数据库中加载fixture,您应该使用所需的数据创建对象(通过使用setter注入它)。

To test controller: load doctrine fixtures and use doctrine transactions so the state of your database is the same before each testcase, begin transaction in setUp() and rollback in tearDow(). (If your controller use transactions too i haven't found a good solution yet).

要测试控制器:加载doctrine fixture并使用doctrine事务,这样在每个测试用例之前数据库的状态是相同的,在setUp()中开始事务并在tearDow()中回滚。 (如果您的控制器也使用了交易,我还没有找到一个好的解决方案)。

For fixtures error, if you don't have any error and your fixtures aren't loaded maybe you have missed a naming convention. Can you show us some code ?

对于灯具错误,如果您没有任何错误并且未加载灯具可能您错过了命名约定。你能告诉我们一些代码吗?

#2


4  

Have a look at this solution. I don't think using transactions is the best idea, since chances are that you'll use transactions in your code. This solution suggests to load fixtures manually in each of your test.

看看这个解决方案。我不认为使用事务是最好的主意,因为您可能会在代码中使用事务。此解决方案建议在每次测试中手动加载灯具。

#3


4  

There is a very handy LiipFunctionalTestBundle that simplifies working with a fixtures in test. The basic idea is to create a database each time you run the tests and then load fixtures. Now you can save the models, delete, every test they will be the same.

有一个非常方便的LiipFunctionalTestBundle,可以简化测试中灯具的使用。基本思想是每次运行测试然后加载fixture时创建一个数据库。现在你可以保存模型,删除,每个测试都是一样的。