【问题标题】:Why are there multiple assertions when running an http test?为什么运行 http 测试时会有多个断言?
【发布时间】:2019-07-09 08:13:40
【问题描述】:

鉴于下面的测试,为什么我收到了预期的 1 个测试的结果,但有 2 个断言都通过了?

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ConvertALeadTest extends TestCase
{
    /** @test */
    public function a_user_can_view_a_convert_page()
    {
        $response = $this->get('/');

        $response->assertRedirect('login');
    }
}

【问题讨论】:

    标签: php laravel phpunit


    【解决方案1】:

    因为assertRedirect 函数有两个断言。一个检查请求是否返回了重定向代码,一个检查结束位置是否正确。

    public function assertRedirect($uri = null)
    {
        PHPUnit::assertTrue(
            $this->isRedirect(), 'Response status code ['.$this->getStatusCode().'] is not a redirect status code.'
        );
    
        if (! is_null($uri)) {
            $this->assertLocation($uri);
        }
    
        return $this;
    }
    

    【讨论】:

    • 啊,太好了,谢谢!我处于“测试”职业生涯的早期,现在感觉就像一个雷区。
    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多