zsolt.domjan's avatar

Object not getting mocked in my test

In my test I'm trying to create the following object mock:

$this->mock(NumbersService::class, function ($mock){ });

For some reason the test fails with this error "Call to a member function instance() on null". First I thought that the issue is the class which I try to mock, so I rebuilt my whole logic to make the class abstract, but still same. I tried it with multiple classes, service classes which work fine in other projects unit tests. Not sure what causes it. Any suggestion is really appreciated.

0 likes
3 replies
Sinnbeck's avatar

Show the test and the code you are testing

Any chance you are using new NumbersService in your code?

zsolt.domjan's avatar

@Sinnbeck No instance of these service classes are instantiated anywhere in my code, they are used as injected dependencies in gateway classes.

zsolt.domjan's avatar
class BuggyTest extends TestCase
{
    public function testSuccessfulAuthentication()
    {
        $sessionId = 'SX9taRonx8yUsKgt6KaR';

        $this->mock(NumbersService::class, function ($mock){ });

     $response = $this->withoutMiddleware()->get('/api/authenticate', [
         'username' => 'test_username',
         'password' => 'test_password'
     ]);

     $response->assertStatus(200);

         $response->assertJson([
            'status'  => 200,
            'message' => 'OK',
            'data' => [
               'generated_session_id' => $sessionId
           ]
       ]);
   }

   public function testNotSuccessfulAuthentication()
    {
       $this->mock(SomeProxyService::class, function ($mock) {
            $mock->shouldReceive('authenticate')->andReturn(false);
        });

       $response = $this->withoutMiddleware()->get('/api/authenticate', []);

       $response->assertStatus(401);

       $response->assertJson([
            'status'  => 401,
            'message' => ' authentication failed'
        ]);
    }
}	

This is the test

class SomeProxyService extends BaseProxy
{
    public function __construct()
    {
        $this->initialize(config('api_url'));
   }

   public function initialize($baseUrl)
    {
        $this->client = new Client([
            'base_uri' => $baseUrl,
            'HTTP_Accept' => 'application/json',
            'verify' => Config::get('ssl_verify'),
            'cookies' => true
        ]);
    }

    public function authenticate($username, $password)
    {
        $authentication = $this->performRequest('/login', 'GET', [
            'query' => [
                'username' => $username,
               'password' => $password,
               'login' => true,
               'jsonlogin' => true
           ]
        ]);

        if($authentication->responsecode !== 200) {
            return false;
        }

        return $authentication->cookies->JSESSIONID ?? false;
    }
}

And this is the class with the 'authenticate' method I'm trying to mock. As I mentioned I tried it with different classes, but no luck. I think the issue is with the test.

Please or to participate in this conversation.