Warm tip: This article is reproduced from serverfault.com, please click

laravel-如何为所有模型/控制器在单个文件中运行phpunit

(laravel - How do I run the phpunit in a single file for all the model/controller)

发布于 2020-12-02 11:24:38

我在使用phpunit时遇到了挑战,该测试可以完美地针对用户输入运行,但无法识别产品功能。我想删除用户身份验证功能以运行产品插入等,但是似乎不起作用。

<?php

namespace Tests\Feature\Auth;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use \App\Models\Product;
use \App\Models\User;
class LoginTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    
    
    public function test_user_can_view_a_login_form(){
        $response = $this->get('user/signin');
        $response->assertStatus(200);
        $response->assertViewIs('user.signin');
    }
    public function test_user_cannot_view_a_login_from_when_authenticated() {

       $user = new User([
           'email' => 'jobtest@test.com',
           'password' => bcrypt($password = 'i-want-this-job'),
       ]);
       if ($user === null) {
           $user->save();
       }
       else{
           echo "User exists";
       }
       
       $response = $this->post('user/signin', [
           'email' => $user->email,
           'password' => $password,
       ]);
      
        $response = $this->actingAs($user)->get('user/signin');
        $this->assertAuthenticatedAs($user);
      
    }

**THE PRODUCT METHODS START FROM HERE** 
    public function user_can_access_home_page() {
        $response = $this->get('/');
        $response->assertStatus(200);
        $response->assertViewIs('/');
     }
   public function can_user_insert_product() {
        $product = new Product([
        'imagePath' => 'https://i0.wp.com/ae01.alicdn.com/kf/HLB1V4vwbjLuK1Rjy0Fhq6xpdFXaJ/Elegant-Women-Dresses-Bodycon-Office-Formal-Business-Work-Party-Sheath-Tunic-Pencil-Midi-O-neck-Short.jpg?fit=800%2C800&ssl=1',
        'title' => 'Elegant Women Dresses',
        'description' => 'Some quick example text to build on the card title and make up the bulk of the content',
        'price' => '8',
        'quantity' => '3'
    ]);  
    $product->save();
    if($product) {
        echo "Product inserted";
    }
  

    $response = $this->post('/', [
        'imagePath' => $product->imagePath,
        'title' => $product->title,
        'description' => $product->description,
        'price' => $product->price,
        'quantity' => $product->quantity,
    ]);
    $response = assertRedirect('/');
}


}

我也曾尝试为其创建另一个测试文件,但仍显示在“ Tests \ Feature \ Auth \ LoginTest”类中​​未找到任何测试。在此处输入图片说明

而当附加到同一测试文件中时,它将运行并忽略乘积方法。还是我需要单独运行php artisan make:test Auth / LoginTest来进行用户登录,并运行php artisan make:test Product / ProductTest,因为我不知道用户为什么会工作而产品不会在此处输入图片说明

Questioner
Payne
Viewed
0
mrhn 2020-12-02 19:42:52

对于phpunit测试运行,他们要么以单词开始test或有@test注解。你的产品测试不遵循这些规则。

通常的解决方案

public function test_can_user_insert_product(){

或注释解决方案

/** @test **/
public function can_user_insert_product