2016-12-14 105 views
0

我开始使用phpunit进行测试,所以我对如何动态测试东西存在疑问。我创建了一个表作为动态波纹管使用phpunit动态按钮href测试

这里,形象是我的观点:

<div class="panel-body"> 
       @if(Session::has('success')) 
       <div class="alert alert-success">{{Session::get('success')}}</div> 
       @endif 
       <table class="table"> 
        <th>Unity Name</th> 
        <th>Phone Number</th> 
        <th>Actions</th> 
        <tbody> 
         @foreach($companies as $company) 
          <tr> 
           <td>{{$company->name}}</td> 
           <td>{{$company->owner_number}}</td> 
           <td> 
           <a href="/admin/company/{{$company->id}}" class="btn btn-default fa fa-newspaper-o"></a> 
           <a href="/admin/company/{{$company->id}}/clients" class="btn btn-default fa fa-users"></a> 
           <a href="company/{{$company->id}}/edit" class="btn btn-default fa fa-pencil-square-o"></a> 
           {{Form::open(['method' => 'DELETE', 'url'=>'/admin/company/'.$company->id, 'style' => 'display:inline'])}} 
            <button type="submit" class="btn btn-default fa fa-trash-o"></button> 
           {{Form::close()}} 
           </td> 
          </tr> 
         @endforeach 
         </tbody> 
        </table> 
       </div> 
      </div> 

所以,我怎么能测试href标记,如果我以前没有标签的ID?

+1

请尝试在此处粘贴您的代码,而不是添加它的图像。 – zuazo

+0

对不起,我后来的回复,所以有我的看法,我在我的表生成 –

回答

0

我在Symfony项目中做了一些像这样的测试。

在开发环境中,我在开发数据库中插入一些数据。完成这一步后,我会启动功能测试。在这些测试中,我分析了标签的内容。以下是您的数据示例:

$client = static::createClient(); 
$crawler = $client->request('GET', '/yourUrl'); 
$node = $crawler->filter('#show-company-1'); 
self::assertNotEmpty($node, "Node #show-company-1 does not exists"); 
self::assertEmpty($node->html(), "#show-company-1 content is not empty"); 
self::assertContains('/admin/company/1', $node->attr('href'), "a#show-company-1.href has not a good value"); 

在请求之前,您可以添加一些逻辑来确定您公司的ID。

+0

首先感谢您的回复!我想过,因为在我的机器测试中,我总是可以将数据库中的第一名作为我的例子,但是如果我没有?我只是在问这种可能性并思考,如果这是一个很好的模式。 –