我在使用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,因为我不知道用户为什么会工作而产品不会
对于phpunit
测试运行,他们要么以单词开始test
或有@test
注解。你的产品测试不遵循这些规则。
通常的解决方案
public function test_can_user_insert_product(){
或注释解决方案
/** @test **/
public function can_user_insert_product
谢谢...我对此表示感谢。我没有注意到这一点。