Skip to content

Commit

Permalink
Sandwich Tests Added
Browse files Browse the repository at this point in the history
  • Loading branch information
PriyeshM1 committed Jun 7, 2017
1 parent c7de2cc commit 84a9e0a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/controllers/SandwichController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package controllers

import com.google.inject.Inject
import play.api.mvc.{Action, Controller}
import services.SandwichService

class SandwichController @Inject()(sandwichService: SandwichService) extends Controller{
def sandwiches() = Action {
val sandwiches = sandwichService.sandwiches
Ok(views.html.sandwiches(sandwiches))
}
}
3 changes: 3 additions & 0 deletions app/models/Sandwich.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package models

case class Sandwich(name: String, price: BigDecimal, description: String)
14 changes: 14 additions & 0 deletions app/services/RealSandwichService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package services

import com.google.inject.ImplementedBy
import models.Sandwich

class RealSandwichService extends SandwichService {
//just an empty list
override def sandwiches(): List[Sandwich] = List()
}

@ImplementedBy(classOf[RealSandwichService])
trait SandwichService {
def sandwiches() : List[Sandwich]
}
13 changes: 13 additions & 0 deletions app/views/sandwiches.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@(sandwiches: List[Sandwich])

@main("Sandwiches") {
<h1>Have a look at today's sandwiches</h1>
@if(sandwiches.isEmpty) {
<p>Sorry, we're sold out</p>
} else {
<p>Please choose a sandwich</p>
@for(s <- sandwiches) {
<li>@s.name, @s.description, £@s.price</li>
}
}
}
1 change: 1 addition & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ GET / controllers.HomeController.index
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)

GET /welcome controllers.WelcomeController.welcome
GET /sandwiches controllers.SandwichController.sandwiches
71 changes: 71 additions & 0 deletions test/controllers/SandwichControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package controllers

import models.Sandwich
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerTest
import play.api.test.FakeRequest
import play.api.test.Helpers.{status, _}
import services.SandwichService

class SandwichControllerSpec extends PlaySpec with GuiceOneAppPerTest {
"SandwichController" should {
"Have some basic information and be accessible at the correct route" in {
// Need to specify Host header to get through AllowedHostsFilter
val request = FakeRequest(GET, "/sandwiches").withHeaders("Host" -> "localhost")
val home = route(app, request).get

//sanitation
status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include("<title>Sandwiches</title>")
contentAsString(home) must include("<h1>Have a look at today's sandwiches</h1>")
}

"give a helpful message when sold out" in {
val controller = new SandwichController(FakeNoSandwichService)
val result = controller.sandwiches().apply(FakeRequest())
contentAsString(result) must include("<p>Sorry, we're sold out</p>")
}

"show a single sandwich when only one is available" in {
val controller = new SandwichController(FakeSingleSandwichService)
val result = controller.sandwiches().apply(FakeRequest())

contentAsString(result) must not include("<p>Sorry, we're sold out</p>")
contentAsString(result) must include ("Ham")
contentAsString(result) must include ("Very tasty")
contentAsString(result) must include ("£1.55")
}

"show multiple sandwiches when more than one is available" in {
val controller = new SandwichController(FakeMultiSandwichService)
val result = controller.sandwiches().apply(FakeRequest())

contentAsString(result) must not include("<p>Sorry, we're sold out</p>")
contentAsString(result) must include ("Ham")
contentAsString(result) must include ("Very tasty")
contentAsString(result) must include ("£1.55")
contentAsString(result) must include ("Cheese")
contentAsString(result) must include ("Cheese tastic")
contentAsString(result) must include ("£2.55")
contentAsString(result) must include ("Egg")
contentAsString(result) must include ("Fresh")
contentAsString(result) must include ("£1.15")
}
}
}

object FakeNoSandwichService extends SandwichService {
override def sandwiches(): List[Sandwich] = List()
}

object FakeSingleSandwichService extends SandwichService {
override def sandwiches(): List[Sandwich] = List(Sandwich("Ham", 1.55, "Very tasty"))
}

object FakeMultiSandwichService extends SandwichService {
val ham = Sandwich("Ham", 1.55, "Very tasty")
val cheese = Sandwich("Cheese", 2.55, "Cheese tastic")
val egg = Sandwich("Egg", 1.15, "Fresh")
override def sandwiches(): List[Sandwich] = List(ham, cheese, egg)
}

0 comments on commit 84a9e0a

Please sign in to comment.