7

假设我有一个控制器,其动作接收两个参数。

它调用两个服务,一个带有每个参数,两个服务都返回字符串

这些字符串中的每一个都作为参数传递给模板

结果被传递给 Ok 并返回。

我想编写一个简单的单元测试来确保: 1 - 使用正确的参数调用正确的服务 2 - 服务的返回值被传递给模板的正确属性

最好的方法是什么?

4

2 回答 2

3

使用带有 Specs2 的 Mockito,我模拟服务以验证它们的方法调用。

我的控制器由 Spring 实例化。这使我可以将其视为 aclass而不是object. => 这对于使controller可测试性至关重要。这里有一个例子:

@Controller
class MyController @Autowired()(val myServices: MyServices) extends Controller

要为控制器启用 Spring,您必须定义一个Global对象,作为 Play!文档解释:

object Global extends GlobalSettings {

  val context = new ClassPathXmlApplicationContext("application-context.xml")

  override def getControllerInstance[A](controllerClass: Class[A]): A = {
    context.getBean(controllerClass)
  }
}

我的单元测试不需要 Spring;我只是将合作者(模拟)传递给构造函数。

然而,关于渲染的模板,我只测试结果的类型(Ok、BadRequest、Redirection 等......)。事实上,我注意到让我的测试详细扫描整个渲染模板(发送给它的参数等)并不容易,只需要单元测试。

因此,为了断言使用正确的参数调用了正确的模板,我相信我运行 Selenium 的验收测试或可能的功能测试,如果您愿意,可以扫描整个预期结果。

2 - 服务的返回值被传递给模板的正确属性

这很容易检查..如何?通过信任编译器!更喜欢将一些自定义类型传递给您的模板,而不是简单的原语,例如: phone: String 将变为:phone: Phone。(一个简单的值对象)。因此,不必担心以非预期的顺序将属性传递给您的模板(在单元测试或实际生产代码中)。编译器确实会发出警告。

这是我使用 specs2 进行的一个单元测试(简化)的示例:(您会注意到包装器的使用:)WithFreshMocks。这case class将允许在测试后刷新所有变量(在这种情况下为模拟)测试。因此是重置模拟的好方法。

    class MyControllerSpec extends Specification with Mockito {

      def is =
        "listAllCars should retrieve all cars" ! WithFreshMocks().listAllCarsShouldRetrieveAllCars

      case class WithFreshMocks() {

        val myServicesMock = mock[MyServices]
        val myController = new MyController(myServicesMock)

        def listAllCarsShouldRetrieveAllCars = {
          val FakeGetRequest = FakeRequest() //fakeRequest needed by controller
          mockListAllCarsAsReturningSomeCars()
          val result = myController.listAllCars(FakeGetRequest).asInstanceOf[PlainResult] //passing fakeRequest to simulate a true request
          assertOkResult(result).
            and(there was one(myServicesMock).listAllCars()) //verify that there is one and only one call of listAllCars. If listAllCars would take any parameters that you expected to be called, you could have precise them.
        }

        private def mockListAllCarsAsReturningSomeCars() { 
           myServicesMock.listAllCars() returns List[Cars](Car("ferrari"), Car("porsche"))
        }

        private def assertOkResult(result: PlainResult) = result.header.status must_== 200

       }
于 2013-07-10T14:26:34.833 回答
2

所以,我想出了一个基于蛋糕图案和模拟的解决方案:

给定服务:

trait Service {
  def indexMessage : String
}

trait ServiceImpl {
  def indexMessage = {
    "Hello world"
  }
}

然后控制器看起来像:

object Application extends ApplicationController
                   with ServiceImpl  {
  def template = views.html.index.apply
}

trait ApplicationController extends Controller
                            with Service {
  def template: (String) => play.api.templates.Html

  def index = Action {
    Ok(template("controller got:" + indexMessage))
  }
}

测试看起来像:

class ApplicationControllerSpec extends Specification with Mockito {
  "cake ApplicationController" should {
      "return OK with the results of the service invocation" in {
        val expectedMessage = "Test Message"
        val m = mock[(String) => play.api.templates.Html]

        object ApplicationControllerSpec extends ApplicationController {
          def indexMessage = expectedMessage
          def template = m
        }

        val response = ApplicationControllerSpec.index(FakeRequest())

        status(response) must equalTo(OK)
        there was one(m).apply(Matchers.eq("controller got:" + expectedMessage))
      }
  }
}

我在让 Mockito 工作时遇到了很多麻烦。
它需要一个额外的依赖,我在解决如何在 scala 中使用匹配器时遇到了很多麻烦(我在 java 中使用它很舒服)

最终我认为上面的答案更好,避免使用 String 和其他可以将它们包装在任务特定类型中的原始类型,然后你会收到编译器警告。

此外,我通常会避免在控制器中添加“控制器得到:”前缀。

在这种情况下它就在那里,所以我可以验证它在现实世界中应该由其他一些组件完成(控制器仅用于管道 IMO)

于 2013-07-10T21:50:12.557 回答