如何测试扩展自定义特征的Play应用程序 [英] How to Test a Play Application that extends a custom trait

查看:77
本文介绍了如何测试扩展自定义特征的Play应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Play混合测试写入我的Play应用程序时遇到麻烦,该应用程序在与Play分开的独立线程中运行.我尝试重写WithApplication.provideApplication方法没有任何运气.我收到一个inheriting conflicting methods错误. (一个来自真实应用程序"MyRunnableSystemWrapper",另一个来自我的 mocked 虚假混入程序,称为"MyMockedSystemWrapper").

I'm having trouble writing tests for a mixin to my Play application that runs in it's own thread separate from play. I've tried over-writing WithApplication.provideApplication method with no luck. I get an inheriting conflicting methods error. (one from the real app "MyRunnableSystemWrapper", one from my mocked fake mixin called "MyMockedSystemWrapper").

execute(system)运行我的系统,该系统在其他地方经过测试并具有副作用(连接到网络服务,因此在不可用时无法通过此测试.好消息是我对系统包装器的模拟服务进行了使用,该包装器使用的系统没有副作用,并且模拟了数据库/网络调用.但是,我不知道如何将我的应用程序的此MOCKED版本进行"WithApplication"测试.

execute(system) runs my system that is tested elsewhere and has sideaffects (connects to networked services, thus failing this test when such things are not available. Good news is I have a mocked service of my system wrapper that uses a system which does NOT have side affects and DB/Network calls are mocked out. However I do not know how to give THIS MOCKED version of my app to "WithApplication" test.

精简代码:

class Application extends Controller with MyRunnableSystemWrapper {

  val pool: ExecutorService = Executors.newFixedThreadPool(1)
  val system = new MyRunnableSystem() //system is abstract in MRSW ^^^ above
  pool.execute(system)

  def index = Action {
    OK("HI")
  }
}

我的测试:

class MyAppTest(implicit ee: ExecutionEnv) extends Specification  {

  abstract class WithMyMockApp extends WithApplication {
    def provideApplication = new controllers.Application with MyMockedSystemWrapper // This imports MyRunnableSystemWrapper 

  }

  "Sending a GET request" should {
    "Respond with OK" in new WithMyMockApp {
      val response = route(app, FakeRequest(GET, "/")).get
      status(response) mustEqual OK
    }
  }
}

如果我没有在正确的位置运行Runnable,并且应该在其他地方调用它以使此测试更容易,请告诉我!

If I'm not running my Runnable in the correct place and should be calling it somewhere else to make this testing easier, let me know!

推荐答案

您可以注入系统包装程序,而不是对其进行扩展

You could inject your system wrapper instead of extending it

trait SystemWrapper {
    def execute(system: RunnableSystem)
}

class MyRunnableSystemWrapper extends SystemWrapper {...}
class MyMockedSystemWrapper extends SystemWrapper {...}

class Application @Inject() (systemWrapper SystemWrapper) extends Controller {

然后,您需要告诉Guice您想要运行时使用哪个SystemWrapper实现,以及要进行测试的哪个实现.一种方法是使用在.conf文件中设置的用于运行时/测试的不同Guice模块.

Then you need to tell Guice which implementation of SystemWrapper you want for runtime and which one for test. One way of doing this is by using different Guice modules for runtime/test which you set in your .conf files.

这篇关于如何测试扩展自定义特征的Play应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆