• <legend id='tGGO1'><style id='tGGO1'><dir id='tGGO1'><q id='tGGO1'></q></dir></style></legend>

      1. <i id='tGGO1'><tr id='tGGO1'><dt id='tGGO1'><q id='tGGO1'><span id='tGGO1'><b id='tGGO1'><form id='tGGO1'><ins id='tGGO1'></ins><ul id='tGGO1'></ul><sub id='tGGO1'></sub></form><legend id='tGGO1'></legend><bdo id='tGGO1'><pre id='tGGO1'><center id='tGGO1'></center></pre></bdo></b><th id='tGGO1'></th></span></q></dt></tr></i><div id='tGGO1'><tfoot id='tGGO1'></tfoot><dl id='tGGO1'><fieldset id='tGGO1'></fieldset></dl></div>
      2. <tfoot id='tGGO1'></tfoot>

        <small id='tGGO1'></small><noframes id='tGGO1'>

        • <bdo id='tGGO1'></bdo><ul id='tGGO1'></ul>

        使用数据库进行 Phpunit 测试

        时间:2023-05-30

        • <i id='H58C1'><tr id='H58C1'><dt id='H58C1'><q id='H58C1'><span id='H58C1'><b id='H58C1'><form id='H58C1'><ins id='H58C1'></ins><ul id='H58C1'></ul><sub id='H58C1'></sub></form><legend id='H58C1'></legend><bdo id='H58C1'><pre id='H58C1'><center id='H58C1'></center></pre></bdo></b><th id='H58C1'></th></span></q></dt></tr></i><div id='H58C1'><tfoot id='H58C1'></tfoot><dl id='H58C1'><fieldset id='H58C1'></fieldset></dl></div>

          <small id='H58C1'></small><noframes id='H58C1'>

          1. <tfoot id='H58C1'></tfoot>
              <tbody id='H58C1'></tbody>
            • <legend id='H58C1'><style id='H58C1'><dir id='H58C1'><q id='H58C1'></q></dir></style></legend>

                <bdo id='H58C1'></bdo><ul id='H58C1'></ul>

                1. 本文介绍了使用数据库进行 Phpunit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 PHPunit 进行单元测试.

                  I am trying to focus a bit on unit testing using PHPunit.

                  我在这里找到了一个非常好的教程http://blog.nickbelhomme.com/php/phpunit-training-course-for-free_282

                  I have found a very good tutorial over here http://blog.nickbelhomme.com/php/phpunit-training-course-for-free_282

                  但是我遗漏了一些东西,我还不知道该怎么做.

                  But there is something I'm missing and don't yet understand how to do.

                  我有一个用户模块,用于维护有关用户的所有信息.并且有一个函数 save 可以将用户保存在数据库中.所以我有一个 testFunction

                  I have a user module which maintains all information about users. And there is a function save which saves the user in the database. So I have a testFunction

                  public function testCanCreateUser()
                  {
                      $userData = array(
                          'userName'  =>  'User1',
                          'firstName' =>  'Joey',
                          'lastName'  =>  'Hendricks',
                          'email'     =>  'Joey@hendricks.com',
                          'password'  =>  'f$tfe8F'
                      
                      ); 
                      $user = new Model_User($userData);
                      $user->save();
                       
                  }
                  

                  我第一次运行测试时,这会起作用.因为数据库是空的.但是当我第二次运行我的测试时它不会工作,因为我的系统不允许同一个用户在数据库中两次.所以为了做到这一点,我必须在每次运行测试之前重新创建我的测试数据库.这样做的最佳方法是什么?

                  The first time when I will run my test this will work. Since the database is empty. But When I run my tests for the second time it won't work since my system doesn't allow the same user twice in the db. So In order to do this I have to recreate my testdatabase every time before I run my tests. What is the best way to do this?

                  或者这个问题可以用不同的方式解决吗?

                  Or is this problem to be solved on a different way?

                  推荐答案

                  如果你想测试你的业务逻辑: Mock掉数据库类并返回假数据

                  If you want to test your business logic: Mock away the Database class and return fake data

                  如果你想测试触发 SQL 语句的类(恕我直言,你也可以测试它,因为我有点想知道我的代码是否可以在后端的真实数据库中正常工作)它会得到有点复杂,但有一些方法可以做到:

                  If you want to test the class that fires the SQL statements (and imho you could test that too since I kinda wanna know if my code works fine with a real db in the backend) it gets a little complicated but there are ways to do it:

                  • 在运行测试之前使用 setUp() 和 tearDown() 为您的数据获得一致的状态是(恕我直言)编写数据库驱动单元测试的好方法.不过,手动编写大量自定义 sql 会很烦人.

                  • Using setUp() and tearDown() to get a consistent state for your data before running your tests is (imho) a fine way to write db-driven unittests. It can get annoying to write lots of custom sql by hand though.

                  为了让您的生活更轻松,您可以查看 DbUnit 扩展 看看这是否适用于您的应用程序.

                  To make your life a little easier you can look into the DbUnit extension and see if that works for your Application.

                  如果您真的想深入了解 Unittesting 数据库交互,关于该主题的最佳读物是(恕我直言)Sebastian Bergmanns phpqa 书.

                  If you really want to dive into Unittesting database interactions the best read on the subject is (imho) the chapter on db-unittesting in Sebastian Bergmanns phpqa book.

                  您的应用程序是否可以允许自定义数据库名称和所有表的自动设置,也可以使用大量测试数据设置一次数据库并在所有测试中使用该数据.尽管一个测试不依赖于另一个测试编写的数据,但您可以小心.

                  Could your application allow for a custom database name and automated setup of all tables it may also be possible to set the db up once with a lot of testdata and use that data in all your tests. You could be carefull so though that one test doesn't rely on data written by another one.

                  这篇关于使用数据库进行 Phpunit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Zend Framework:控制器对象中的 init() 和 preDispatch() 函数有什么区别? 下一篇:Zend 框架 2 推荐的错误处理方式

                  相关文章

                  <i id='sSVQF'><tr id='sSVQF'><dt id='sSVQF'><q id='sSVQF'><span id='sSVQF'><b id='sSVQF'><form id='sSVQF'><ins id='sSVQF'></ins><ul id='sSVQF'></ul><sub id='sSVQF'></sub></form><legend id='sSVQF'></legend><bdo id='sSVQF'><pre id='sSVQF'><center id='sSVQF'></center></pre></bdo></b><th id='sSVQF'></th></span></q></dt></tr></i><div id='sSVQF'><tfoot id='sSVQF'></tfoot><dl id='sSVQF'><fieldset id='sSVQF'></fieldset></dl></div>

                  1. <small id='sSVQF'></small><noframes id='sSVQF'>

                    <tfoot id='sSVQF'></tfoot>
                      <bdo id='sSVQF'></bdo><ul id='sSVQF'></ul>
                  2. <legend id='sSVQF'><style id='sSVQF'><dir id='sSVQF'><q id='sSVQF'></q></dir></style></legend>