phpMick's avatar
Level 15

Test for message after redirect.

Hi,

How can I test that I get this message:

public function doSomething()
{

if($ok){

  return view('message-page')
            ->with('title','The message?')
}

}

My test just looks like this:

$foo->doSomething();

What can I assert to check the message that is passed to the view?

0 likes
7 replies
phpMick's avatar
Level 15

Yes, I would usually use ->AssertSee() chained onto the end of the command.

I does not work for this situation. If I try:

$foo->doSomething()->assertSee('script');

I get Method [assertSee] does not exist on Redirect.

And If I try just placing it after the function call:

$response->assertSee('script');

I get: Undefined variable: response

I need to do a function call not a GET because I am passing a mocked object

Here is my test so far:

public function do_the_test()
{
     //Arrange
        
        $mockObject = Mockery::mock('App\MockObject');

        $authCode = 123456;

        $mockObject->shouldReceive('doSomething')->once()->with($authCode)->andReturn([
            'code' => '123456',
            'name' => 'joe',
            'username' => 'smith',
            'email' => '[email protected]',
            'domain' => 'yahoo.co.uk'
        ]);

       
    //Act
        $object = new Foo($mockObject);
        $object->doSomething();
        
    //Assert
        ??
        //check that view is returned with message text
}

Mick

ohffs's avatar

Maybe dd() the view you get back - it'll give you the object type then you can see what is available on it? That's how I end up figuring some of this out... :-)

phpMick's avatar
Level 15

If I do this:

$test = $object->doSomething();

$test is a RedirectResponse

ohffs's avatar

Then there's something in your doSomething() method that's returning it - or there's something the object is inheriting/etc (or using a middleware - whatever) that's doing a redirect. Try putting dd's into the doSomething() method to see where the code is going.

phpMick's avatar
Level 15

I pasted the code above, this is the redirect and this is what I need to test:

return view('message-page')
            ->with('title','The message?')

I have stepped through with Xdebug and this line gets executed correctly, I just need to somehow assert this in my test.

Mick

phpMick's avatar
Level 15

I have hacked this by setting a session variable (instead of passing the messages with the view) and then checking that with assertEquals();

Would be nice to find a better way. I just think that the problem here is my lack of Mockery knowledge/experience.

Please or to participate in this conversation.