doufu6504 2016-02-16 09:36
浏览 57

在另一个类中使用语句

I am going through a Laracast tutorial which suggest me to have the statement use DatabaseTransactions; inside my test class definition, to allow the data changes in my test case will persists only for that case. I have a test class,

    Class MyClassTest extends TestCase
    {

    // This use statement is responsible for 
    //all the data operations to persists only with in that test case
    use DatabaseTransactions;
     /**
     * @test
     */  
     public function my_test_function()
    {
     // My test case code. Which inserts/updates 
     //data and assert statement.
    }
    }

The use DatabaseTransactions works in a way that the data changes in the method persists only with in that. How that works exactly?

  • 写回答

1条回答 默认 最新

  • dongliuliu0385 2016-02-16 13:01
    关注

    As went further, DatabaseTransactions is trait, which contains the method beginDatabaseTransaction with @before annotation.

    • Having an use DatabaseTransactions within the test class lets the class have that method.
    • Since the method beginDatabaseTransaction contains a @before annotation, it will be executed before every test case.
    • The below is the method definition which begins a transaction and rolls back existing transaction before the test case finishes(it is still unclear why @before should be called before the test case finishes).

      @before

      public function beginDatabaseTransaction() {

      $this->app->make('db')->beginTransaction();
      
      $this->beforeApplicationDestroyed(function () {
          $this->app->make('db')->rollBack();
      });
      

      }

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类