bslinger's avatar

Tests wrapped in transactions make it hard to test actual transactional behaviour

I'm just getting my head around doing some integration testing using Codeception and Laravel 5, taking inspiration from the 'Integration Testing Repositories' Laracast.

My issue is that I am attempting to test a function which creates and persists an object to the database (an Order), but as the Order has multiple related objects (Address, Items), the whole thing is done in a transaction to ensure that if one of the related objects is invalid, nothing is saved.

This is working fine when I run my code, but when I am writing tests for it the tests fail because the rollback does not occur due to the fact that there is already a transaction open for the test itself.

Is there a way to test this without just turning off transactions for my tests? Or can I turn off transactions for just this test?

Here is my test code in case it helps:

    public function it_does_not_save_order_when_relation_is_invalid()
    {
        $this->orderData = TestHelper::preSaveOrderData();
        unset($this->orderData["items"][0]['sku']);

        $this->tester->cantSeeRecord('orders', ['display_id' => $this->orderData["display_id"]]);
        try
        {
            $this->order = $this->creator->CreateOrder($this->orderData);
        }
        catch(InvalidOrderException $e)
        {
            $this->tester->cantSeeRecord('orders', ['display_id' => $this->orderData["display_id"]]);
        }
    }
0 likes
0 replies

Please or to participate in this conversation.