Summer Sale! All accounts are 50% off this week.

zachleigh's avatar

Testing redirect back started failing

I have several tests to test redirect back for form input error handling. They were working fine, but now they all fail. Heres the error message:

1) DictionaryEnjpTest::test_search_for_unknown_input_redirects_back_with_error
Did not land on expected page [http://localhost/dictionary/enjp].

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dictionary/enjp'
+'http://localhost'

So for some reason, the tests are redirecting to '/' instead redirecting back to the previous address.

Heres one of the failing tests:

    public function test_search_for_empty_input_redirects_back_with_error()
    {
        $this->visit('dictionary/enjp')
             ->type('', 'input')
             ->press('Search')
             ->seeStatusCode(200)
             ->seePageIs('dictionary/enjp');

        $this->assertEquals(session('error_message'), Lang::get('errors.search.empty'));
    }

And the controller method that deals with this redirect:

    /**
     * Search and display results
     *
     * @return Redirect
     */
    public function search()
    {
        $data = $this->request->all();

        $input = $data['input'];

        if (empty($input)) {
            return back()->withInput()->with('error_message', Lang::get('errors.search.empty'));
        }
       
        $results = $this->apiSearch($input);

        $answer = $results['answer'];
        $query = $results['query'];

        if ($answer->isEmpty()) {
            return back()->withInput()->with('error_message', Lang::get('errors.search.no_result'));
        }

        if (count($answer) === 1) {
            $word = getWordFromAnswer($answer);

            return redirect('/dictionary/enjp/' . $word);
        }

        $answer = $this->result_transformer->transformCollection($answer);

        session(['answer' => $answer, 'query' => $query]);

        return redirect('dictionary/enjp/results');
    }

In the browser, it works fine. Until today, it worked fine. Any idea what happened? I updated my computer, if that makes any difference....

0 likes
4 replies
zachleigh's avatar

Anybody? Because this works fine in the browser, Im guessing that there is some kind of problem with how the testing suit deals with redirects when the tests are running. How can I track down the cause of this error?

EDIT: I went through my whole app and changed all instances of back() to redirect() with a route specified. All my tests now pass. The test suite doesnt like the back() method for some reason.

zachleigh's avatar
zachleigh
OP
Best Answer
Level 47

Found a good solution. Use $this->call() to access the route and send a custom HTTP_REFERER value. Then simply check to make sure you were redirected to the HTTP_REFERER route.

    /**
     * Search with empty input redirects back.
     * 
     * @test
     */
    public function search_for_empty_input_redirects_back_with_error()
    {
        $this->call('POST', '/pagesearch', ['input' => ''], [], [], ['HTTP_REFERER' => '/results']);

        $this->assertRedirectedTo('/results');
    }
3 likes
martin88's avatar

I have a similar issue and I was looking for a good solution.

After reading your post, I think instead of setting the referer manually, another solution would be, just to go first to the page, from where the post request is fired. Something like this.

    $this->get(route('form'));
    $response = $this->post(route('form.store', ['ba'=>'bumm']));
    $response->assertStatus(302);
    $response->assertRedirect(route('form'));
2 likes
Taelkir's avatar

Another option is to use the ->from() method to tell Laravel where you want ->back() to redirect to. i.e.

/** @test */
public function redirects_back() 
{
		$this->from('/page-one')->get('/redirects-back');
		$this->assertRedirectedTo('/page-one');
}
1 like

Please or to participate in this conversation.