Laravel功能测试。缓存模拟不起作用! CacheManager :: driver(),但未指定期望

时间:2019-05-21 09:05:21

标签: laravel testing mocking phpunit laravel-5.8

当我使用单元测试缓存模拟测试我的应用程序时,效果很好。但是,当我尝试通过功能测试来测试api时,却出现了异常。

public function testGet()
{
    Cache::shouldReceive('rememberForever')->times(5)->andReturn([]);

    Cache::shouldReceive('has')
        ->once()
        ->andReturn(false);

    Cache::shouldReceive('forever')
        ->once()
        ->andReturn([]);

    $response = $this->getJson('/api/table/get');
    $response->assertOk();

    $responseArr = $response->getOriginalContent();
    $this->assertEmpty($responseArr['table']);
}
  

Tests \ Feature \ Controller \ API \ Site \ TableApiControllerTest :: testGet   Mockery \ Exception \ BadMethodCallException:已收到   Mockery_2_Illuminate_Cache_CacheManager :: driver(),但没有期望   被指定

在其他单元测试中完全相同的模拟也可以正常工作。

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案-https://www.elastic.co/guide/en/elasticsearch/reference/master/discovery-settings.html

import pytest

@pytest.fixture(scope="function")  # <-- want this to be scope="module"; run once!
@pytest.mark.asyncio
async def connection(event_loop):
    """ Expensive function; want to do in the module scope. Only this function needs `event_loop`!
    """
    conn await = make_connection(event_loop)
    return conn


@pytest.mark.dependency()
@pytest.mark.asyncio
async def test_insert(connection, event_loop):  # <-- does not need event_loop arg
    """ Test insert into database.

        NB does not need event_loop argument; just the connection.
    """
    _id = 0
    success = await connection.insert(_id, "data")
    assert success == True


@pytest.mark.dependency(depends=['test_insert'])
@pytest.mark.asyncio
async def test_find(connection, event_loop):  # <-- does not need event_loop arg
    """ Test database find.

        NB does not need event_loop argument; just the connection.
    """
    _id = 0
    data = await connection.find(_id)
    assert data == "data"