Support the ongoing development of Laravel.io →
posted 10 years ago
Testing
Last updated 2 years ago.
0

assertRedirectedToRoute works fine for me. I suspect that the problem here is in the way you've constructed the route or are triggering it in your test.

In case it helps, the following example works for me:

//  routes.php

Route::get('redirect', array('as' => 'users.index', function()
{
    return 'users.index';
}));

Route::post('redirect', function()
{
    return Redirect::route('users.index');
});

// test.php

public function testRedirect()
{
    $this->call('POST', 'redirect');

    $this->assertRedirectedToRoute('users.index');
}
Last updated 2 years ago.
0
//routes.php
Route::resource('users' , 'UsersController');

//Controller
public function store(){
	$input = Input::all();

	$validator = Validator::make($input, User::$rules);

	if ($validator->fails()) {
		return Redirect::route('users.create')->withErrors($validator)->withInput();
	}

	unset($input['password_confirmation']);
	$input['password'] = Hash::make($input['password']);

	$this->users->create($input);
	
	return Redirect::route('users.index')->with('message', 'Thanks for siging up!');
}

//test	
public function testStorePass(){
	Validator::shouldReceive('make')
		->once()
		->andReturn(Mockery::mock(['fails' => false]));

	$mock = Mockery::mock('Illuminate\Auth\UserInterface');
	$mock->shouldReceive('create')->once();
	App::instance('Illuminate\Auth\UserInterface', $mock);

	Input::replace($input = [
		'firstname' => 'Example',
		'lastname' => 'Test',
		'email' => '[email protected]',
		'username' => 'test',
		'password' => 'password',
		'password_confirmation' => 'password'
		]);

	$this->call('POST', 'users', $input);

	$this->assertRedirectedTo('users');
	$this->assertRedirectedToRoute('users.index');
}

This is the code in my project I think there has to be a setting or so for testing where the routes are created and there it probably takes localhost in front of it.. I guess it is not that important to fix this but sadly I am a perfectionist xD

Last updated 2 years ago.
0

I have some suggestions with your code here: http://laravel.io/bin/9YOz

Last updated 2 years ago.
0

Thank you! I will look into it but they way you suggest to test store without moking users->create you would actually store a new user in your DB which is bad if you are just testing => you need some mokes. And I am not testing the validator since I am mocking it's behavior as well.

Last updated 2 years ago.
0

Good catch... yeah you can mock User (or whatever $this->user is) and verify that create was called. I was just pointing out that your test seemed brittle to me. Is it necessary that a validation check happened? If you are testing the positive, then it can be assumed that validation passed. You would also want to test the negative where validation fails, which is why I suggested pulling the validation into a method on $this->user, then you can test it in isolation, for its positive and negative affirmations

Last updated 2 years ago.
0

I have two tests because if validation fails I should get redirected differently. And since I mock the Validator I am not really testing the Validator in this test. But still the redirecttoroute is not working as intended....

Last updated 2 years ago.
0

I am starting to believe there is something wrong with my phpunit.xml file

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

d3orn d3orn Joined 10 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.