Upgrade to Pro — share decks privately, control downloads, hide ads and more …

自動テストの2歩目

chiroruxx
February 03, 2020

 自動テストの2歩目

2020/02/03 タピオカLTで発表する資料です。

chiroruxx

February 03, 2020
Tweet

More Decks by chiroruxx

Other Decks in Technology

Transcript

  1. ಉ͡ϩδοΫ function sum(array $array) { $sum = 0; foreach ($array

    as $value) { $sum += $value; } return $sum; } public function testSum() { $input = [1, 2, 3]; $expected = 0; foreach ($input as $value) { $expected += $value; } $this->assertEquals($expected, sum($input)); }
  2. ܾΊଧͪςετ public function testSum() { $this->assertEquals(6, sum([1, 2, 3])); $this->assertEquals(2,

    sum([2])); $this->assertEquals(0, sum([0, 0, 0])); $this->assertEquals(0, sum([])); } public function testSum() { $input = [1, 2, 3]; $expected = 0; foreach ($input as $value) { $expected += $value; } $this->assertEquals($expected, sum($input)); }
  3. σʔλϓϩόΠμ public function testSum(array $input, bool $expected) { $this->assertEquals($expected, sum($input));

    } public function dataProvider(): array { return [ ['input' => [1, 2, 3], 'expected' => 6], ['input' => [2], 'expected' => 2], ['input' => [0, 0, 0], 'expected' => 0], ['input' => [], 'expected' => 0], ]; }
  4. σʔλϓϩόΠμͷ࢖͍·Θ͠ public function testGetQuestionsWithoutDate(array $input, array $expected) { $expected['created_at'] =

    null; $expected['updated_at'] = null; $expected['deleted_at'] = null; $service = new QuestionService(); $this->assertEquals($expected, $service->getQuestionsWithoutDate($input)); }
  5. ςετσʔλϓϩόΠμ public function getQuestionDataProvider(): array { return [ 'input' =>

    [ 'title' => 'question title', ], 'expected' => [ 'title' => 'question title', 'author' => ‘default author', 'created_at' => '2019-02-03 00:00:00', 'updated_at' => '2019-02-03 00:00:00', 'deleted_at' => null, ], ]; } public function getQuestionWithoutDateDataProvider(): array { return [ 'input' => [ 'title' => 'question title', ], 'expected' => [ 'title' => 'question title', 'author' => ‘default author', ], ]; }
  6. ςετσʔλϓϩόΠμ public function testGetQuestions(array $input, array $expected) { $service =

    new QuestionService(); $this->assertEquals($expected, $service->getQuestions($input)); } public function testGetQuestionsWithoutDate(array $input, array $expected) { $service = new QuestionService(); $this->assertEquals($expected, $service->getQuestionsWithoutDate($input)); }
  7. ςετσʔλϓϩόΠμ public function getQuestionDataProvider(): array { return [ 'input' =>

    [ 'title' => 'question title', ], 'expected' => $this->defaultQuestionAttributes(), ]; } public function defaultQuestionAttributes(array $overrides = []) { $default = [ 'title' => 'question title', 'author' => 'default author', 'created_at' => '2019-02-03 00:00:00', 'updated_at' => '2019-02-03 00:00:00', 'deleted_at' => null, ]; return array_merge($default, $overrides); }
  8. ϥϯμϜ஋ͷݕূ public function testDelete() { $question1 = factory(Question::class)->create(); $question2 =

    factory(Question::class)->create(); $service = new QuestionService(); $service->delete($question1); $this->assertDatabaseMissing(['title' => $question1->title]); $this->assertDatabaseHas(['title' => $question2->title]); }
  9. ϥϯμϜ஋ͷݕূ public function testDelete() { $question1 = factory(Question::class)->create(['title' => 'ফ͑Δ']);

    $question2 = factory(Question::class)->create(['title' => '࢒Δ']); $service = new QuestionService(); $service->delete($question1); $this->assertDatabaseMissing(['title' => 'ফ͑Δ']); $this->assertDatabaseHas(['title' => '࢒Δ']); }