所以我尝试为一个laravel应用程序编写单元测试,如下所示:
protected function setUp()
{
parent::setUp();
$this->disableMiddlewareForAllTests();
$this->withoutExceptionHandling();
}
public function test_login_badRequest()
{
$response = $this->call('post', '/login');
$response->assertOk();
$response->assertJson(['status' => 400]);
}
上面的工作正常,并提供以下输出:
Test 'Tests\Unit\LoginTest::test_login_badRequest' started
Test 'Tests\Unit\LoginTest::test_login_badRequest' ended
Time: 69 ms, Memory: 12.00MB
OK (1 test, 2 assertions)
但下一个测试是另一个故事:
public function test_login_goodRequest()
{
$response = $this->call('post', '/login', [
'email' => 'email@email.com',
'password' => 'P4ssw0rd'
]);
$response->assertOk();
$response->assertJson(['status' => 200]);
}
一旦测试命中应用程序中的这一行代码:
/* Retrieve User associated with posted email address */
$user = $this->userModel
->where('email', $request->post('email'))
->get();
die('here');// <-- Happens in browser, does not happen in tests.
这是错误:
1) Tests\Unit\LoginTest::test_login_goodRequest
PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
/var/www/html/projects/laravel-template/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68 ...
几天来我一直在用我的头撞这个,你知道这是怎么回事吗,怎么让那个模具在测试中打印出来?干杯。
--laradock,docker,nginx,mysql,php 7.2.4-1+ubuntu16.04.1+deb.sury.org+1(cli)(构建时间:2018年4月5日08:53:57)(nts)版权所有(c)1997-2018 the php group zend engine v3.2.0,版权所有(c)1998-2018 zend technologies with zend opcache v7.2.4-1+ubuntu16.04.1+deb.sury.org+1,版权所有(c)1999-2018,由zend technologies提供
编辑时间:
.环境:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Z+p6uA3G2DyCCFBXOPgrWwls1U6z/vi8Zi2r4eKHxEY=
APP_DEBUG=true
DEBUGBAR_ENABLED=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=sample_database
DB_USERNAME=root
DB_PASSWORD=root
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
1条答案
按热度按时间42fyovps1#
在聊天中,我们发现您正在从主机运行测试。可以从主机上运行测试,但必须向主机公开端口。我认为laradock做到了,尽管我不确定你的设置中是否有它(github版本有)。不过,这是 Docker 的反模式。
容器被设计成包含它们之间的所有通信以及来自主机的所有通信
up
以及down
.因此,最好的解决办法是进入
workspace
集装箱docker exec -it workspace bash
或者docker-compose exec workspace bash
然后在那里进行测试。