{"id":283,"date":"2020-12-24T09:58:53","date_gmt":"2020-12-24T09:58:53","guid":{"rendered":"http:\/\/robinluo.top\/?p=283"},"modified":"2020-12-24T10:04:40","modified_gmt":"2020-12-24T10:04:40","slug":"laravel-http-test-%e9%9b%86%e6%88%90%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=283","title":{"rendered":"laravel http test \u96c6\u6210\u6d4b\u8bd5"},"content":{"rendered":"\n<p>http \u6d4b\u8bd5 (\u5728feature\u662f\u96c6\u6210\u6d4b\u8bd5  \u5728unit\u662f\u5355\u5143\u6d4b\u8bd5 \u60f3\u8981\u8dd1\u8d77laravel\u7684 \u8981\u5728feature\u91cc\u505a\u6d4b\u8bd5)<\/p>\n\n\n\n<p>\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nnamespace Tests\\Feature;\r\n\r\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\r\nuse Illuminate\\Foundation\\Testing\\WithoutMiddleware;\r\nuse Tests\\TestCase;\r\n\r\nclass ExampleTest extends TestCase\r\n{\r\n    \/**\r\n     * A basic test example.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function test_a_basic_request()\r\n    {\r\n        $response = $this->get('\/');\r\n\r\n        $response->assertStatus(200);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>mock \u8bf7\u6c42\u5934<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    \/**\n     * A basic functional test example.\n     *\n     * @return void\n     *\/\n    public function test_interacting_with_headers()\n    {\n        $response = $this->withHeaders(&#91;\n            'X-Header' => 'Value',\n        ])->post('\/user', &#91;'name' => 'Sally']);\n\n        $response->assertStatus(201);\n    }\n}<\/code><\/pre>\n\n\n\n<p>cookie<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    public function test_interacting_with_cookies()\n    {\n        $response = $this->withCookie('color', 'blue')->get('\/');\n\n        $response = $this->withCookies(&#91;\n            'color' => 'blue',\n            'name' => 'Taylor',\n        ])->get('\/');\n    }\n}<\/code><\/pre>\n\n\n\n<p>session<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    public function test_interacting_with_the_session()\n    {\n        $response = $this->withSession(&#91;'banned' => false])->get('\/');\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8eab\u4efdmocking<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse App\\Models\\User;\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    public function test_an_action_that_requires_authentication()\n    {\n        $user = User::factory()->create();\n\n        $response = $this->actingAs($user)\n                         ->withSession(&#91;'banned' => false])\n                         ->get('\/');\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$this->actingAs($user, 'api') \/\/\u5173\u952e\u662f\u8fd9\u53e5<\/code><\/pre>\n\n\n\n<p>\u6253\u5370\u54cd\u5e94<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\nuse Illuminate\\Foundation\\Testing\\WithoutMiddleware;\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    \/**\n     * A basic test example.\n     *\n     * @return void\n     *\/\n    public function testBasicTest()\n    {\n        $response = $this->get('\/');\n\n        $response->dumpHeaders();\n\n        $response->dumpSession();\n\n        $response->dump();\n    }\n}<\/code><\/pre>\n\n\n\n<p>json api \u6d4b\u8bd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>?php\n\nnamespace Tests\\Feature;\n\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    \/**\n     * A basic functional test example.\n     *\n     * @return void\n     *\/\n    public function test_making_an_api_request()\n    {\n        $response = $this->postJson('\/api\/user', &#91;'name' => 'Sally']);\n\n        $response\n            ->assertStatus(201)\n            ->assertJson(&#91;\n                'created' => true,\n            ]);\n    }\n}\n$this->assertTrue($response&#91;'created']);\r\n<\/code><\/pre>\n\n\n\n<p>\u5224\u65adjson\u7ed3\u679c\u5b8c\u5168\u4e00\u6837<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    \/**\n     * A basic functional test example.\n     *\n     * @return void\n     *\/\n    public function test_asserting_an_exact_json_match()\n    {\n        $response = $this->json('POST', '\/user', &#91;'name' => 'Sally']);\n\n        $response\n            ->assertStatus(201)\n            ->assertExactJson(&#91;\n                'created' => true,\n            ]);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5224\u65adjson\u5d4c\u5957\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    \/**\n     * A basic functional test example.\n     *\n     * @return void\n     *\/\n    public function test_asserting_a_json_paths_value()\n    {\n        $response = $this->json('POST', '\/user', &#91;'name' => 'Sally']);\n\n        $response\n            ->assertStatus(201)\n            ->assertJsonPath('team.owner.name', 'Darian');\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6587\u4ef6\u4e0a\u4f20mocking<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace Tests\\Feature;\n\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\nuse Illuminate\\Foundation\\Testing\\WithoutMiddleware;\nuse Illuminate\\Http\\UploadedFile;\nuse Illuminate\\Support\\Facades\\Storage;\nuse Tests\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    public function test_avatars_can_be_uploaded()\n    {\n        Storage::fake('avatars');\n\n        $file = UploadedFile::fake()->image('avatar.jpg');\n\n        $response = $this->post('\/avatar', &#91;\n            'avatar' => $file,\n        ]);\n\n        Storage::disk('avatars')->assertExists($file->hashName());\n    }\n}\n\/\/Storage::disk('avatars')->assertMissing('missing.jpg');\r \/\/\u6587\u4ef6\u4e0d\u5b58\u5728<\/code><\/pre>\n\n\n\n<p>\u6240\u6709\u7684response assert\u5224\u65ad<\/p>\n\n\n\n<p><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-cookie\">assertCookie<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-cookie-expired\"> assertCookieExpired<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-cookie-not-expired\"> assertCookieNotExpired<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-cookie-missing\"> assertCookieMissing<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-created\"> assertCreated<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-dont-see\"> assertDontSee<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-dont-see-text\"> assertDontSeeText<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-exact-json\"> assertExactJson<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-forbidden\"> assertForbidden<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-header\"> assertHeader<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-header-missing\"> assertHeaderMissing<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json\"> assertJson<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-count\"> <\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-count\">assertJsonCount<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-fragment\"> assertJsonFragment<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-missing\"> assertJsonMissing<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-missing-exact\"> assertJsonMissingExact<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-missing-validation-errors\"> assertJsonMissingValidationErrors<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-path\"> assertJsonPath<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-structure\"> assertJsonStructure<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-json-validation-errors\"> assertJsonValidationErrors<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-location\"> assertLocation<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-no-content\"> assertNoContent<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-not-found\"> assertNotFound<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-ok\"> assertOk<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-plain-cookie\"> assertPlainCookie<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-redirect\"> assertRedirect<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-see\"> assertSee<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-see-in-order\"> assertSeeInOrder<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-see-text\"> assertSeeText<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-see-text-in-order\"> assertSeeTextInOrder<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-has\"> assertSessionHas<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-has-input\"> assertSessionHasInput<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-has-all\"> assertSessionHasAll<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-has-errors\"> assertSessionHasErrors<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-has-errors-in\"> assertSessionHasErrorsIn<\/a> <a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-has-no-errors\">assertSessionHasNoErrors<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-doesnt-have-errors\"> assertSessionDoesntHaveErrors<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-session-missing\"> assertSessionMissing<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-status\"> assertStatus<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-successful\"> assertSuccessful<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-unauthorized\"> assertUnauthorized<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-view-has\"> assertViewHas<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-view-has-all\"> assertViewHasAll<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-view-is\"> assertViewIs<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/http-tests#assert-view-missing\"> assertViewMissing<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>http \u6d4b\u8bd5 (\u5728feature\u662f\u96c6\u6210\u6d4b\u8bd5 \u5728unit\u662f\u5355\u5143\u6d4b\u8bd5 \u60f3\u8981\u8dd1\u8d77laravel\u7684 \u8981\u5728feature\u91cc\u505a\u6d4b\u8bd5) \u4f8b\u5b50 mock \u8bf7\u6c42\u5934 cookie session \u8eab\u4efdmocking \u6253\u5370\u54cd\u5e94 json api \u6d4b\u8bd5 \u5224\u65adjson\u7ed3\u679c\u5b8c\u5168\u4e00\u6837 \u5224\u65adjson\u5d4c\u5957\u5bf9\u8c61 \u6587\u4ef6\u4e0a\u4f20mocking \u6240\u6709\u7684response assert\u5224\u65ad assertCookie assertCookieExpired assertCookieNotExpired assertCookieMissing assertCreated assertDontSee assertDontSeeText assertExactJson assertForbidden assertHeader assertHeaderMissing assertJson assertJsonCount assertJsonFragment assertJsonMissing assertJsonMissingExact assertJsonMissingValidationErrors assertJsonPath assertJsonStructure assertJsonValidationErrors assertLocation assertNoContent assertNotFound assertOk assertPlainCookie assertRedirect assertSee assertSeeInOrder assertSeeText assertSeeTextInOrder assertSessionHas assertSessionHasInput assertSessionHasAll assertSessionHasErrors [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[37,36,35],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/283"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":3,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}