Created
March 21, 2012 16:32
-
-
Save pocketberserker/2149340 to your computer and use it in GitHub Desktop.
ScalaTodoListのテストコード試し書き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import org.specs2._ | |
import specification._ | |
import play.api.test._ | |
import play.api.test.Helpers._ | |
class ApplicationSpec extends Specification with AroundExample { def is = | |
"Applicationオブジェクトのテスト" ^ | |
p^ | |
"Applicationオブジェクトは" ^ | |
"'/'に接続したとき" ^ | |
"ステータスはSee Otherである" ! index().s^ | |
"'/tasks'にリダイレクションされる" ! index().r^ | |
endp^ | |
"初期状態で'/tasks'に接続したとき" ^ | |
"ステータスはOKである" ! initTasks().s^ | |
"文字コードはutf-8である" ! initTasks().char^ | |
"コンテンツ内に'0 task(s)'が含まれている" ! initTasks().h1^ | |
endp^ t^ | |
"何も入力しないでタスクを登録すると" ^ | |
"'/newTask'のステータスはBad Requestである" ! bad().s^ | |
endp^ t^ | |
"タスクを1つ登録すると" ^ | |
"コンテンツ内に'1 task(s)'が含まれている" ! haveOne().h1^ | |
"コンテンツ内に登録したタスクのラベルが含まれている" ! haveOne().data^ | |
endp^ | |
"タスクが1つ登録された状態で'/tasks'に接続したとき" ^ | |
"タスクを一つ削除して'/tasks'に接続すると" ^ | |
"コンテンツ内に'0 task(s)'が含まれている" ! delete().h1^ | |
"コンテンツ内に削除したタスクのラベルが含まれていない" ! delete().nodata^ | |
end | |
case class index() { | |
val actual = controllers.Application.index(FakeRequest()) | |
def s = status(actual) must equalTo(SEE_OTHER) | |
def r = redirectLocation(actual) must beSome.which(_ == "/tasks") | |
} | |
case class initTasks() { | |
val actual = controllers.Application.tasks(FakeRequest()) | |
def s = status(actual) must equalTo(OK) | |
def char = charset(actual) must beSome.which(_ == "utf-8") | |
def h1 = contentAsString(actual) must contain("0 task(s)") | |
} | |
case class bad() { | |
val actual = controllers.Application.newTask( | |
FakeRequest().withFormUrlEncodedBody("label" -> "") | |
) | |
def s = status(actual) must equalTo(BAD_REQUEST) | |
} | |
case class haveOne() { | |
val label = "test" | |
// When | |
controllers.Application.newTask( | |
FakeRequest().withFormUrlEncodedBody("label" -> label) | |
) | |
// Verify | |
val actual = controllers.Application.tasks(FakeRequest()) | |
def h1 = contentAsString(actual) must contain ("1 task(s)") | |
def data = contentAsString(actual) must contain (label) | |
} | |
case class delete() { | |
val label = "test" | |
// Given | |
controllers.Application.newTask( | |
FakeRequest().withFormUrlEncodedBody("label" -> label) | |
) | |
//When | |
controllers.Application.deleteTask(1)(FakeRequest()) | |
//Verify | |
val actual = controllers.Application.tasks(FakeRequest()) | |
def h1 = contentAsString(actual) must contain ("0 task(s)") | |
def nodata = contentAsString(actual) must not contain (label) | |
} | |
def around[R <% execute.Result](r: =>R) = running(FakeApplication(additionalConfiguration = inMemoryDatabase()))(r) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import org.specs2._ | |
import specification._ | |
import play.api.test._ | |
import play.api.test.Helpers._ | |
import models._ | |
class TaskSpec extends Specification with AroundExample { def is = | |
"Taskオブジェクトのテスト" ^ | |
p^ | |
"タスクが1つも登録されていないとき" ^ | |
"タスク数は0であり" ! empty().size^ | |
"タスクリストは空である" ! empty().nil^ | |
endp^ t^ | |
"タスクを1つ登録すると" ^ | |
"タスク数は1であり" ! haveOne().size^ | |
"追加したラベルを持つタスクがタスクリストに含まれている" ! haveOne().contain^ | |
endp^ | |
"タスクが1つ登録されているとき" ^ | |
"先頭のタスクを削除すると" ^ | |
"タスク数は0であり" ! delete().size^ | |
"タスクリストは空である" ! delete().nil^ | |
end | |
case class empty() { | |
val actual = Task.all() | |
def size = actual must have size(0) | |
def nil = actual must be empty | |
} | |
case class haveOne() { | |
val label = "test" | |
// When | |
Task.create(label) | |
// Verify | |
val actual = Task.all() | |
def size = actual must have size(1) | |
def contain = actual must have contain Task(1, label) | |
} | |
case class delete() { | |
// Given | |
Task.create("test") | |
// When | |
Task.delete(1) | |
// Verify | |
val actual = Task.all() | |
def size = actual must have size(0) | |
def nil = actual must be empty | |
} | |
def around[R <% execute.Result](r: =>R) = running(FakeApplication(additionalConfiguration = inMemoryDatabase()))(r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment