Jeff Lambert
Jeff Lambert

Reputation: 24661

Assert specific view returned from controller route

Assuming I have a controller method that looks something like this:

public function someRoute()
{
    if(some condition) {
        return View::make('view1');
    }

    return View::make('view2');
}

How would I assert in my unit test that view1 was returned as opposed to view2? A colleague mentioned that if I can get the HTML response as a string then I can just use PHPUnit's assertRegExp against the returned HTML to match a given string only found in view1, but that doesn't seem right to me.

Is there a better way? A deeper question may be should I even need to worry about which view is returned in my unit test or if I should just $this->assertResponseOk() ?

Upvotes: 1

Views: 311

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87779

You are mixing unit test with acceptance tests, so you have two options:

1) Split those tests to unit and acceptance, and use a tool like Codeception to help you do acceptance, which is way more elegant than PHPUnit for this kind of test. With Codecption you would do things like:

$I->amOnPage('/someUrl');

$I->see('John Doe');

2) Do what your friend told you to do.

Upvotes: 2

Related Questions