【问题标题】:Laravel - Test Edit Form with Value tagLaravel - 带有值标签的测试编辑表单
【发布时间】:2019-03-22 20:39:54
【问题描述】:

我正在使用 Laravel 5.6 开发一个网站。

目前,我想为网站编写测试代码。一般来说,我也是建立网站的新手,这是我了解自己做错了什么的学习曲线。

我基于用户模型创建了一个配置文件,该配置文件只能由经过身份验证的用户编辑。

表单在浏览器端实际上可以正常工作,但是一旦我运行 phpunit,它就会失败。

测试脚本:

/** @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);
}

控制器:

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!');
    }
}

查看:

<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>

注释掉的表单测试的图像: Commented Form

未注释的表单测试的图像: Not commented Form

我很困惑为什么一旦我插入带有值标签的表单我的测试就会失败。如果我注释掉表单或者只是删除值标签,测试就会通过。

这几天一直在寻找,仍然找不到正确的答案。我使用正确的断言吗?我在这里想念什么?任何输入都将帮助我进一步理解这一点。谢谢!

【问题讨论】:

    标签: php laravel phpunit laravel-5.6


    【解决方案1】:

    我找到了答案。它实际上是我创建的工厂。

    在用户模型中,每次注册都会导致创建一个空配置文件。

    这是我编写测试脚本的新方法:

    /** @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']);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 2015-03-28
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多