【问题标题】:How do I run the phpunit in a single file for all the model/controller如何在单个文件中为所有模型/控制器运行 phpunit
【发布时间】:2021-03-14 08:33:01
【问题描述】:

我在使用 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('/');
}


}

我也尝试为它创建另一个测试文件,但仍然显示 No tests found in class "Tests\Feature\Auth\LoginTest"。

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

【问题讨论】:

    标签: laravel phpunit


    【解决方案1】:

    要运行phpunit 测试,它们要么以单词test 开头,要么具有@test 注释。您的产品测试不遵循这些规则。

    通常的解决方案

    public function test_can_user_insert_product(){
    

    或者注解解决方案

    /** @test **/
    public function can_user_insert_product
    

    【讨论】:

    • 谢谢...我很感激。我没有注意到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多