mardi 9 février 2021

Mockery not returning specified value

Test

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\Models\User;
use App\Models\Course;

class PaymentControllerTest extends TestCase
{
    use DatabaseMigrations;

    protected $user;
    protected $course;
    protected $payment_gate_mock;
    
    protected function setUp()
    {
        parent::setUp();

        $this->user = factory(User::class)->create();
        $this->course = factory(Course::class)->create();

        $this->payment_gate_mock = \Mockery::mock('App\Http\Controllers\Library\PaymentGate');

        // I'm letting Laravel know that this class here will be mocked.
        $this->app->instance('App\Http\Controllers\Library\PaymentGate', $this->payment_gate_mock);

    }

    /** @test */
    public function a_user_can_pay_for_the_course()
    {
        // This is not working
        $this->payment_gate_mock->shouldReceive('place_order')->andReturn(1);
        
        $params = [
            'course_id' => $this->course->id,
            'staff' => 0,
            'ccnumber' => 4111111111111111,
            'month' => 10,
            'year' => 25,
            'cvv' => 999
        ];
        
        $this->actingAs($this->user)
            ->post("/payment/course", $params)
            ->assertStatus(200);
    }
}

Controller

<?php

namespace App\Http\Controllers\Web;

...

class PaymentController extends Controller
{
    public function make_course_payment( CoursePaymentRequest $request )
    {
        ...

        $payment = new PaymentGate(...);

        // This should always return 1 in the test but it doesn't
        $result = $payment->place_order();

       if( $result['response'] == 1) {
           ....
       }
    }
}

I'm trying to write a test for a controller which makes a request call to a third party service via a custom class called PaymentGate.

I'm trying to get this $this->payment_gate_mock->shouldReceive('place_order')->andReturn(1); stub to work so that I can test different scenarios for the request... but it actually attempts to make a request for some reason.

Am I missing something in my test that prevents Mockery from mocking the place_order method?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/36X0eRJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire