SteveAzz's avatar

Test doesn't work but in browser it work. Laravel 5 & Laracasts\Integrated

I have the following test.

    public function it_updates_post()
    {
        $post = TestDummy::create('Blog\Post');
        $newPost = ['title' => 'Some title', 'body' => 'The body'];


        $this->visit('posts/' . $post->id . '/edit')
            ->seePageIs('posts/' . $post->id . '/edit')
            ->see('Edit Post')
            ->submitForm('Update Post', $newPost);
    }

When I run the test it goes me the following error `A GET request to 'http://localhost/posts/218' failed. Got a 405 code instead.

MethodNotAllowedHttpException on RouteCollection.php line 207 `

When I try and update a post inside of the browser it works as intended. What can be causing this?

0 likes
11 replies
mstnorris's avatar

It doesn't look like you're using the right METHOD ?

You say that you're sending a GET request to http://localhost/posts/218, surely it should either be a POST or PATCH request.

In your case from the look of your test, you are creating a new blog post, so you need POST method.

SteveAzz's avatar

My test fials when i add the "SubmitForm" method other than that it works. I dont know why it is failing in the test but in the browser it works.

mstnorris's avatar

Can I see the form? You might have named the input fields incorrectly meaning the validation fails.

SteveAzz's avatar
<!-- title From Input -->
<div class="row control-group">
    <div class="form-group col-xs-12 floating-label-form-group controls">
        {!! Form::label('title', 'Enter the title of the post:') !!}
        {!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Enter the title of the post']) !!}
    </div>
</div>

<!-- body From Input -->
<div class="row control-group">
    <div class="form-group col-xs-12 floating-label-form-group controls">
        {!! Form::label('body', 'Enter the body of the post:') !!}
        {!! Form::textarea('body', null, ['class' => 'form-control', 'rows' => '20' , 'placeholder' => 'Enter the body of the post']) !!}
    </div>
</div>

<!-- tagsList From Input -->
<div class="row control-group">
    <div class="form-group col-xs-12 floating-label-form-group controls">
        {!! Form::label('tagList', 'Select an Option:') !!}
        {!! Form::select('tagList[]', $tags, null, ['id' => 'tags', 'class' => 'form-control', 'multiple']) !!}
    </div>
</div><br/>

<!-- Submit Button -->
<div class="row">
    <div class="form-group col-xs-12">
        {!! Form::submit($submitButton, ['class' => 'btn btn-default']) !!}
    </div>
</div>

The tag field is optional

mstnorris's avatar

@Colossus

Why aren't you using TestDummy again, this is what you have currently. You're not passing any tags through either when you create the test which is why I think it fails, even though it is optional, I would still suggest to use TestDummy again (see below).

public function it_updates_post()
{
    $post = TestDummy::create('Blog\Post');
    $newPost = ['title' => 'Some title', 'body' => 'The body'];


    $this->visit('posts/' . $post->id . '/edit')
        ->seePageIs('posts/' . $post->id . '/edit')
        ->see('Edit Post')
        ->submitForm('Update Post', $newPost);
}

Instead use TestDummy when creating a new Blog Post.

public function it_updates_post()
{
    $post = TestDummy::create('Blog\Post');
    $newPost = TestDummy::create('Blog\Post'); // why not use TestDummy again?


    $this->visit('posts/' . $post->id . '/edit')
        ->seePageIs('posts/' . $post->id . '/edit')
        ->see('Edit Post')
        ->submitForm('Update Post', $newPost);
}
SteveAzz's avatar

@mstnorris The tag is optinal. I was using the test dummy but it was still failing so i dicded to hard code the values just in case something was wrong with testdummy

SteveAzz's avatar
 {!! Form::model($post, ['method' => 'PATCH', 'action' => ['PostsController@update', $post->id]]) !!}
        @include('posts.form', ['submitButton' => 'Update Post'])
    {!! Form::close() !!}

I think is something wrong with my test, because in the browser it works perfectly.

SteveAzz's avatar

I have figured it out! I overriding the SetUp method so I can log in and it apperly is brakes the submitForm method. Weird stuff...

AnotherSamPower's avatar

Hey @SteveAzz, currently trying to test form submits and have run into the same issue as you. Did you ever find a work around to testing the forms?

Please or to participate in this conversation.