madsem
1 month ago
136
1
Testing

Tests individually green, run together red... Ideas?

Posted 1 month ago by madsem

Hey Guys,

Can anybody tell me why these tests turn green when run individually, but when run together in sequence the last one (fallback), turns red?

<?php

namespace Tests\Feature\Console\Commands;

use Tests\TestCase;
use Mockery\MockInterface;
use App\Models\ExchangeRate;
use Illuminate\Support\Facades\File;
use App\Clients\Support\ExchangeRateClient;
use Illuminate\Foundation\Testing\RefreshDatabase;

/**
 * @see \App\Console\Commands\DownloadExchangeRatesCommand
 */
class DownloadExchangeRatesCommandTest extends TestCase
{

    use RefreshDatabase;

    /**
     * @test
     */
    public function it_saves_exchange_rate_model_from_primary_endpoint()
    {
        $this->partialMock(ExchangeRateClient::class, function (MockInterface $mock) {

            $response = File::get(base_path('tests/__Fixtures__/ExchangeRateApi/exchangeratesapi.io.success.json'));

            $mock->shouldReceive('getLatestRates->queryEndpoint->collectResponse')
                 ->with($response);
        });

        $this->artisan('download-exchange-rates')
             ->assertExitCode(0)
             ->run();

        $this->assertNotNull(ExchangeRate::find(1));
    }


    /**
     * @test
     */
    public function it_saves_exchange_rate_model_from_fallback_endpoint()
    {
        $this->partialMock(ExchangeRateClient::class, function (MockInterface $mock) {

            $response = File::get(base_path('tests/__Fixtures__/ExchangeRateApi/frankfurter.app.success.json'));

            $mock->shouldReceive('getLatestRates->queryEndpoint->collectResponse')
                 ->with($response);
        });

        $this->artisan('download-exchange-rates')
             ->assertExitCode(0)
             ->run();

        $this->assertNotNull(ExchangeRate::find(1));
    }

}

PHPUnit 9.3.10 by Sebastian Bergmann and contributors.

Failed asserting that null is not null. /opt/project/tests/Feature/Console/Commands/DownloadExchangeRatesCommandTest.php:58

Please sign in or create an account to participate in this conversation.