如何使用Scalatra,Specs2,Mockito重置模拟调用计数器

约美

我从没想到我会在这个网站上问一个问题,因为一切都已经正常回答了,但是使用Scalatra ...我没有找到很多信息,所以这里是:

我没有足够的经验,所以也许我错过了一些东西,但是据我了解,如果我想测试我在Scalatra上开发的API,每次运行测试套件时都需要启动服务器,对吗?

第二个问题,如何重置方法的调用计数器,这样我就不必计算自测试套件开始以来该方法被调用了多少次?现在使用此功能可以给我带来不止一个的好处,因为它可以算上一次测试。

there was one(*instance*).*method*(*parameter*)

我仍然可以通过计算或暂时将测试作为第一个测试来解决问题,但这不是可持续的解决方案...

我发现的另一件事:重置模拟方法...未找到http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#17

在类范围内隔离测试:我们需要添加

val servlet = new Servlet(eventRepoMock)
addServlet(servlet, "/*")

而且我们不能在每次初始化时都重复addServlet https://etorreborre.github.io/specs2/guide/SPECS2-3.5/org.specs2.guide.Isolation.html

我尝试的最后一件事是:

servlet.repo = mock[EventRepo]

但是repo作为一个值,我不能像这样改变它。

这些“解决方案”都不是很干净,所以我想知道是否有人有一个能解决这个问题的天才想法!

先感谢您 !

编辑:感谢埃里克的评论,上面的问题解决了(很容易),但是现在我有问题了,因为我正在测试异步调用的get / post,因此重置模拟不会在正确的时间进行...任何建议?

这是代码的简化版本:

class EventServiceSpec extends ScalatraSpec with Mockito with Before { def is = s2"""
Event Service

GET an existing event
must return status 200                              $get_status200
must return the event with id = :id                 $get_rightEventElement
must call findById once on the event repository     $get_findByIdOnRepo
"""

lazy val anEvent = Event(1, "Some Event"
lazy val eventsBaseUrl = "/events"
lazy val existingEventUrl = s"$eventsBaseUrl/${anEvent.id}"

lazy val eventRepoMock = mock[EventRepository]

lazy val servlet = new Servlet(eventRepoMock)
addServlet(servlet, "/*")

def before = {
    eventRepoMock.findById(anEvent.id) returns Option(anEvent)
    eventRepoMock.findById(unexistingId) returns None
    eventRepoMock.save(anEvent) returns Option(anEvent)
}

def get_status200 = get(existingEventUrl){
    status must_== 200
}

def get_findByIdOnRepo = get(existingEventUrl){
    // TODO count considering previous test... need to find a cleaner way
    there was three(eventRepoMock).findById(anEvent.id)
}
埃里克

所有org.mockito.Mockito功能仍可以在specs2规范中使用,并且reset是其中之一。

现在,由于您要在多个示例中共享模拟对象的状态,因此,您不仅需要在每个示例之前重设模拟状态,还需要制定规范sequential

class EventServiceSpec extends ScalatraSpec with Mockito 
  with BeforeAll with BeforeEach { 
  def is = sequential ^ s2"""
  Event Service

  GET an existing event
    must return status 200                              $get_status200
    must return the event with id = :id                 $get_rightEventElement
    must call findById once on the event repository  $get_findByIdOnRepo
  """

  lazy val anEvent = Event(1, "Some Event")
  lazy val eventsBaseUrl = "/events"
  lazy val existingEventUrl = s"$eventsBaseUrl/${anEvent.id}"

  lazy val eventRepoMock = mock[EventRepository]

  lazy val servlet = new Servlet(eventRepoMock)

  def beforeAll = addServlet(servlet, "/*")

  def before = {
    reset(eventRepoMock)
    eventRepoMock.findById(anEvent.id) returns Option(anEvent)
    eventRepoMock.findById(unexistingId) returns None
    eventRepoMock.save(anEvent) returns Option(anEvent)
  }

  def get_status200 = get(existingEventUrl){
    status must_== 200
  }

  def get_findByIdOnRepo = get(existingEventUrl){
   there was one(eventRepoMock).findById(anEvent.id)
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章