douyanyan1123 2014-09-03 13:34
浏览 41

强制behat在curl请求期间使用laravel 4.2在内存db中使用

I'm trying to get some Behavior tests integrated into my API test suite. I'm leveraging Laravel 4.2 and already have a nice suite of unit tests. The problem I'm running into is persistent data after these tests suite run - as well as correctly populated seed data.

I've tried to link sqlite into my bootstrap process following a few examples I've seen in various places online, but all this is really doing is setting up my DB when I call behat from CLI - during the application run (most specifically any curl requests out to the API) laravel still ties my DB to my local configuration which uses a mysql db.

here's an example snippet of a test suite, the Adding a new track scenario is one where I'd like to have my API use the sqlite when the request and payload is made to my API.

Feature: Tracks

    Scenario: Finding all the tracks
        When I request "GET /api/v1/tracks"
        Then I get a "200" response

    Scenario: Finding an invalid track
        When I request "GET /api/v1/tracks/1"
        Then I get a "404" response

    Scenario: Adding a new track
        Given I have the payload:
             """
             {"title": "behat add", "description": "add description", "short_description": "add short description"}
             """                             
         When I request "POST /api/v1/tracks"
         Then I get a "201" response

Here is a snippet of my bootstrap/start.php file. What I'm I am trying to accomplish is for my behat scenario (ie: Adding a new track) request to hit the testing config so I can manage w/ a sqlite db.

$env = $app->detectEnvironment(array(
    'local' => array('*.local'),
    'production' => array('api'),
    'staging' => array('api-staging'),
    'virtualmachine' => array('api-vm'),
    'testing' => array('*.testing'),
));
  • 写回答

1条回答 默认 最新

  • dongtiao0657 2014-10-06 21:40
    关注

    Laraval does not know about Behat. Create a special environment for it, with its own database.

    Here is what I have in my start.php:

    if (getenv('APP_ENV') && getenv('APP_ENV') != '') 
    {
        $env = $app->detectEnvironment(function()
        {
                return getenv('APP_ENV');
        });
    } 
    else 
    {
        $env = $app->detectEnvironment(array(
    
                'local' => array('*.local', 'homestead'),
                /* ... */
        ));
    }
    

    APP_ENV is set in your Apache/Nginx VirtualHost config. For apache:

    SetEnv APP_ENV acceptance
    

    Create a special local test URL for Behat to use and put that in the VirtualHost entry.

    I recommend using an SQLite file-based database. Delete the file before each Feature or Scenario. Found it to be much quicker than MySQL. I want to use the SQLite in-memory mode but I could not find a way to persist data between requests with the in-memory database.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值