简体   繁体   中英

Laravel sanctum unit testing

I am trying to make a unit test case for user registration. So I created test_user_registration for testing.

public function test_user_registration(){
     $this->withoutExceptionHandling();
     $this->withoutMiddleware();
     Bus::fake();
     Sanctum::actingAs(
         User::factory()->create(),
         ['*']
     );
     $response = $this->post('/api/users',[
         "firstName"=>"Kamal",
         "lastName"=>"Garu",
         "email"=>"Kamasll@gmail.com",
         "roleId"=>[4],
         "defaultRoleId"=>4,
         "investorId"=>[15],
         "defaultInvestorId"=>15,
         "streetAddress"=>"Jalur Banj",
         "postalCode"=>"977",
         "city"=>"bhaktapur",
         "countryId"=>160,
         "phone"=>9741801138,
         "status"=>1,
         "emailPortfolioReport"=>0,
         "emailInvestorActivities"=>0,
         "emailMarketUpdate"=>1,
         "isdCodeId"=>149,
         "roleType"=>["investorInternational"]
     ]);
     VerifyUser::dispatch($response);
     Bus::assertDispatched(VerifyUser::class);
     $response->assertStatus(200);
 }

When i run this test it returns Illuminate\\Auth\\Access\\AuthorizationException: not authorized.

Since i used Sanctum::actingAs why this error is fetch?? Please help

Check the permission of your route. Maybe it is available only to users with specific roles such as admin or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM