Scala:模拟伴随对象&模型与服务方法 [英] Scala: Mock companion object & Model vs Service approach

查看:96
本文介绍了Scala:模拟伴随对象&模型与服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了多个有关如何模拟伴侣对象的问题,根据 doc a>对于版本3,嘲笑的伴侣对象仍标记为将来.

I have seen multiple questions regarding as how to mock a companion object with frequently mentioned answer being to use scalamock but according to the doc for version 3, mocking companion objects is still marked for future.

那么模拟伴侣对象的方式是什么?如果没有,还有比我已经启用测试更好的方法吗?

So what is the way to mock a companion object? If not is there a better way to do things than what I already have to enable testing?

使用播放框架的代码:

模型

case class Article(id: String, preview: String)

object Article {
   def get(id: String) = {
     ......
   }
}

ArticleResource

class ArticleResource extends Controller {
   def getArticle(id: String) = authenticate {
      ......
      Article.get(id)
      ......
   }
}

如何模拟Article.get(id)?

在工作之前,我对代码的结构进行了不同的修改:

Before when I had it working, I had structured the code differently:

工作测试方法

模型

case class Article(id: String, preview: String)

服务

class ArticleService {
   def get(id: String) = {
     ......
   }
}

object ArticleService {
   def apply = new ArticleService
}

ArticleResource

class ArticleResource(articleService: ArticleService) {
   def getArticle(id: String) = authenticate {
      ......
      articleService.get(id)
      ......
   }
}

object ArticleResource extends controllers.ArticleResource(ArticleService())

在测试中,我在资源中注入了mock[ArticleService]进行测试.

In the test I injected a mock[ArticleService] into the Resource for testing.

我一直在听以前的方法(模型)是正确"的方法,我从来不明白为什么会这样,所以我尝试了一下并遇到了测试问题,但我仍然不明白为什么它是更好的方法. (考虑到测试的困难,实际上我对后一种方法的感觉更加强烈)

I kept hearing the former approach (models) is the "right" approach, I never understood why so I tried it and ran into problems into testing and still I do not understand why it is the better approach. (I am actually feeling more strongly towards the latter approach now considering the difficulties with testing)

所以我有2个问题:

  1. 有没有办法模拟伴侣对象?
  2. 在代码的结构上是否存在正确"的方法?如果可以,为什么?

推荐答案

1)我认为这是不可能的,而且我也不认为这是个好主意.我认为将您需要模拟的行为放在伴侣对象中是不可取的.

1) I don't think it's possible, and I don't think it's a good idea. I don't think it's desirable to put behavior-that-you-need-to-mock in companion objects.

2)我也相信您的第一种方法更好.如果您希望对当前代码进行最少的更改,那该怎么做:

2) I also believe that your first approach was better. If you want to keep your current code with minimal changes, how about that:

case class Article(id: String, preview: String)

object Article {
   def get(id: String) = {
     ......
   }
}

class ArticleResource(articleFinder: (String) => Article = Article.get) {
   def getArticle(id: String) = authenticate {
      ......
      articleFinder(id)
      ......
   }
}

在测试ArticleResource时,您可以仅创建一个函数String =>您选择的Article.您甚至不需要一个框架:)

And when testing ArticleResource you can just create a function String => Article of your choice. You don't even need a framework for that :)

您甚至可以将伴随对象的方法用作默认值,以使其更易于实例化生产代码.

You can even use the companion object's method as default value to make it easier to instantiate in production code.

这篇关于Scala:模拟伴随对象&模型与服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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