Permalink
Browse files

建立單元測試 PostServiceTest

  • Loading branch information...
1 parent b9016de commit 84ebcd8db360c865bbe8b7e01cc3e0f6818d5de4 @oomusou committed Jul 12, 2016
Showing with 50 additions and 0 deletions.
  1. +50 −0 tests/PostServiceTest.php
@@ -0,0 +1,50 @@
+<?php
+
+use App\Post;
+use App\Services\PostService;
+use Illuminate\Foundation\Testing\DatabaseMigrations;
+
+class PostServiceTest extends TestCase
+{
+ use DatabaseMigrations;
+
+ /** @test */
+ public function 有資料取title欄位資料()
+ {
+ /** arrange */
+ collect(range(1, 3))->each(function ($value) {
+ factory(Post::class)->create([
+ 'title' => 'title' . $value
+ ]);
+ });
+
+ /** act */
+ $id = 1;
+ $default = 'no title';
+ $actual = app(PostService::class)->showTitle($id, $default);
+
+ /** assert */
+ $expected = 'title1';
+ $this->assertEquals($expected, $actual);
+ }
+
+ /** @test */
+ public function 無資料的title欄位資料()
+ {
+ /** arrange */
+ collect(range(1, 3))->each(function ($value) {
+ factory(Post::class)->create([
+ 'title' => 'title' . $value
+ ]);
+ });
+
+ /** act */
+ $id = 4;
+ $default = 'no title';
+ $actual = app(PostService::class)->showTitle($id, $default);
+
+ /** assert */
+ $expected = 'no title';
+ $this->assertEquals($expected, $actual);
+ }
+}

0 comments on commit 84ebcd8

Please sign in to comment.