besrabasant's avatar

Writing Test for resource Controller method

Hello folks, I am new to testing so please help me figure out this.

I have a resource controller which has a store method

public function store(CreateClientRequest $request)
    {
                   
        $this->client->create( $request->only(['name', 'address','primary_email','contact_no']) );

        return redirect()->route('clients.show',['id' => $this->client->id]);
    }

$this->client is a model Client that is injected in the __construct method of the controller.

Now, How should I write test for this?

I tried writing a test but it does'nt pass.

Here's the test

public function storeAddsNewClient()
    {
    $clientsMock = \Mockery::mock('Eloquent','App\Client');
    $this->app->instance('App\Client',$clientsMock);

        $client = factory(Client::class)->make();

        $input = [
            'name' => $client->name,
            'address' => $client->address,
            'primary_email' => $client->primary_email,
            'contact_no' => $client->contact_no
        ];

        $clientsMock
            ->shouldReceive('create')
            ->once()
            ->with($input);

        $response = $this->post('/clients', $input);
        
           $response->assertStatus(302)
                ->assertRedirect(route('clients.show'));
    }

Am I doing anything wrong???

0 likes
0 replies

Please or to participate in this conversation.