dongshao8471 2018-10-18 04:50
浏览 106

Laravel - 带有Value标签的测试编辑表单

I'm using Laravel 5.6 to develop a website.

Currently, I want to write a test codes for the website. I'm also new to building a website in general and this is learning curve for me to learn what I'm doing wrong.

I created a Profile based on a User model and the Profile should only be editable by the authenticated User only.

The form is actually working without errors on the browser side but once i run phpunit, it will fail.

Test Script:

/** @test */
public function an_authenticated_user_can_view_the_profile_page()
{
    // Generate fake instance of authenticated user
    $this->be($user = factory('App\User')->create());

    // Will get the URL
    $response = $this->get('/profile/'.$user->name);

    // Check whether the string exists
    $response->assertSee('Personal details for '.$user->name);
}

Controller:

class ProfileController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function show(User $user)
    {
        return view('user.profiles.show', compact('user'));
    }

    public function update(Request $request)
    {
        $this->validate(request(), [
            'company' => 'required',
            'street' => 'required',
            'city' => 'required',
            'zip_code' => 'required',
            'state' => 'required',
            'country' => 'required',
            'phone' => 'required',
        ]);

        $profile = \Auth::user()->profile;

        $profile->update($request->all());

        return back()->with('success', 'Profile updated!');
    }
}

View:

<div class="heading">
    <h3 class="text-uppercase">Personal details for {{ $user->name }}</h3>
</div>

<form method="POST" action="/profile">

{{method_field('PATCH')}}

{{csrf_field()}}

    <input type="hidden" value="{{ $user->profile->id }}" name="id">

    <div class="col-md-6">
        <div class="form-group">
            <label for="company">Company</label>
            <input id="company" type="text" class="form-control" name="company" value="{{ $user->profile->company }}" required>
        </div>
    </div>
</form>

Image of the commented out Form test: Commented Form

Image of the not commented Form test: Not commented Form

I am rather confused why my test is failing once I insert the form with a value tag. If i commented out the form or just remove the value tag, the test will pass.

Been searching for the few days and still can't find the right answer to this. Am i using the right Assertion? What am I missing here? Any inputs will help me to further understand this. Thanks!

  • 写回答

1条回答 默认 最新

  • dqdmvg7332 2018-10-18 10:13
    关注

    I found the answer. It was actually the factory that I've created.

    In the User model, every registration leads to creating an empty Profile.

    This is the new way of how I write the test script:

    /** @test */
    public function an_authenticated_user_can_view_the_profile_page()
    {
        //Generate a fake profile
        $profile = factory('App\Profile')->create();
    
        // Assign it to the user
        $user = $profile->user;
    
        // Authenticate the user
        $this->be($user);
    
        // Will get the URL
        $response = $this->get('/profile/'.$user->name);
    
        // Check whether the string exists
        $response->assertSee('Personal details for '.$user['name']);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计