I think because the dot (special char) in the name, can you check if this is the case?
A User Has a Profile Episode Test Fail due to certain usernames, works in browser for names without special character.
I followed this https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/22 and created user profiles, when tested in browser some profiles with names such as "John Doe" appear and work fine but other with names like "Mr. Alphonso Osinski" seem to get [Not Found] page with this message: [The requested resource /user/Mr.%20Alphonso%20Osinski was not found on this server] due to which my tests fail.
Please help.
Yes, all usernames with "." fail to load profile.
I don't know how Jeff's all test pass as he also may have such names when generated through "php artisan tinker". Can you figure out why? Please help. @mvd
@harshitbatra what you can do is change the factory to create a dummy user to avoid the dots.
In file database/factories/modelfactories.php (or in newer versions, UserFactory.php), change
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
....
to
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->firstName . ' ' . $faker->lastName,
....
It doesn't solve the issue, still a "not found" page on running the test. I updated the factory, did php artisan migrate: fresh to make sure it was taken into account. Still, test fails.
Whats is the user name where you get a 'not found page' ? And can you give us the user factory code?
UserFactory
<?php
use App\Answer;
use App\Question;
use App\Subject;
use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->firstName . ' ' . $faker->lastName,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
});
$factory->define(Question::class, function (Faker $faker) {
return [
'user_id' => function(){
return factory('App\User')->create()->id;
},
'qnop' => $faker->sentence,
'qtitle' => $faker->sentence,
'qdetails' => $faker->sentence,
'subject_id' => function(){
return factory('App\Subject')->create()->id;
}
];
});
$factory->define(Answer::class, function (Faker $faker) {
return [
'user_id' => function(){
return factory('App\User')->create()->id;
},
'question_id' => function(){
return factory('App\Question')->create()->id;
},
'rating' => $faker->numberBetween($min = 0, $max = 5),
'ans' => $faker->sentence
];
});
$factory->define(Subject::class, function (Faker $faker) {
$name = $faker->word;
return [
'subject' => $name,
'subslug' => $name
];
});
Test that fails:
/** @test */
public function user_can_profile()
{
$user = factory('App\User')->create();
$this->get('/user/{$user->name}')
->assertSee($user->name);
}
Error:
1) Tests\Feature\ProfilesTest::user_can_profile
Failed asserting that '<!DOCTYPE html>\n
<html lang="en">\n
<head>\n
<meta charset="utf-8">\n
<meta name="viewport" content="width=device-width, initial-scale=1">\n
\n
<title>Not Found</title>\n
\n
<!-- Fonts -->\n
<link rel="dns-prefetch" href="//fonts.gstatic.com">\n
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">\n
\n
<!-- Styles -->\n
<style>\n
html, body {\n
background-color: #fff;\n
color: #636b6f;\n
font-family: 'Nunito', sans-serif;\n
font-weight: 100;\n
height: 100vh;\n
margin: 0;\n
}\n
\n
.full-height {\n
height: 100vh;\n
}\n
\n
.flex-center {\n
align-items: center;\n
display: flex;\n
justify-content: center;\n
}\n
\n
.position-ref {\n
position: relative;\n
}\n
\n
.code {\n
border-right: 2px solid;\n
font-size: 26px;\n
padding: 0 15px 0 15px;\n
text-align: center;\n
}\n
\n
.message {\n
font-size: 18px;\n
text-align: center;\n
}\n
</style>\n
</head>\n
<body>\n
<div class="flex-center position-ref full-height">\n
<div class="code">\n
404 </div>\n
\n
<div class="message" style="padding: 10px;">\n
Not Found </div>\n
</div>\n
</body>\n
</html>\n
' contains "Sylvia Zieme".
C:\Users\harsh\saf\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:345
C:\Users\harsh\saf\tests\Feature\ProfilesTest.php:20
The test is failing for all user names. It's when I go through the browser, the ones with special character give the "Not Found" page while others work perfectly.
Please or to participate in this conversation.