0

我有一个使用 Cake 模式的 Scala 应用程序:

trait RepositoryComponent {
  def repository: Repository

  trait Repository {
    def shouldSave(record: GenericRecord): Boolean
    def findRecord(keys: Array[String]): Long
    def insertRecord(record: GenericRecord)
    def updateRecord(keys: Array[String], record: GenericRecord)
    def cleanUp()
  }
}

trait DbRepositoryComponent extends RepositoryComponent with Logged {
  val connection: Connection
  val subscription: Subscription
  val schema: Schema

  def repository = new DbRepository(connection, subscription, schema)

  class DbRepository(connection: Connection, subscription: Subscription, schema: Schema) extends Repository {
    ...
  }

trait ConsumerComponent {
  def consumer: Consumer

  trait Consumer {
    def write(keys: Array[String], record: GenericRecord)
    def close()
  }
}

trait DbReplicatorComponent extends ConsumerComponent with Logged {
  this: RepositoryComponent =>

  def consumer = new DatabaseReplicator()

  class DatabaseReplicator extends Consumer {
    ...
  }

当我天真地开始测试 的实现时consumer.write,我尝试了这样的事情(使用 ScalaMock):

class DbReplicatorComponentTests extends FunSuite with MockFactory {

  private val schema = new Schema.Parser().parse(getClass.getResourceAsStream("/AuditRecord.avsc"))
  private val record = new GenericRecordBuilder(schema)
    .set("id1", 123)
    .set("id2", "foo")
    .set("text", "blergh")
    .set("audit_fg", "I")
    .set("audit_ts", 1498770000L)
    .build()

  test("A record should be inserted if the audit flag is 'I' and no existing record is found") {
    val replicator = new DbReplicatorComponent with RepositoryComponent {
      override def repository: Repository = stub[Repository]
    }

    (replicator.repository.shouldSave _).when(record).returns(true)
    (replicator.repository.findRecord _).when(Array("123", "foo")).returns(0L)

    replicator.consumer.write(Array("123", "foo"), record)

    (replicator.repository.insertRecord _).verify(record)
  }
}

测试失败,因为我没有对实际实现进行存根:

Unsatisfied expectation:

Expected:
inAnyOrder {
  <stub-1> Repository.shouldSave({"id1": 123, "id2": "foo", "text": "blergh", "audit_fg": "I", "audit_ts": 1498770000}) any number of times (never called)
  <stub-2> Repository.findRecord([Ljava.lang.String;@4b8ee4de) any number of times (never called)
  <stub-4> Repository.insertRecord({"id1": 123, "id2": "foo", "text": "blergh", "audit_fg": "I", "audit_ts": 1498770000}) once (never called - UNSATISFIED)
}

Actual:
  <stub-3> Repository.shouldSave({"id1": 123, "id2": "foo", "text": "blergh", "audit_fg": "I", "audit_ts": 1498770000})
ScalaTestFailureLocation: com.generalmills.datalake.sql.DbReplicatorComponentTests at (DbReplicatorComponentTests.scala:12)
org.scalatest.exceptions.TestFailedException: Unsatisfied expectation:

这个问题突出了一个事实,即我真的不了解 Scala。我不知道 stub-3 是从哪里来的。根据 damirv 在这个问题中的回答,我已经成功地重新设计了我的测试,但我希望在这里得到一些见解,以帮助我更好地理解为什么我实际上并没有在上面的测试中嘲笑我认为我的想法?

Fwiw,我从 C# 背景来到 Scala。

更新:根据下面的答案,这有效:

test("A record should be inserted if the audit flag is 'I' and no existing record is found") {
    val replicator = new DbReplicatorComponent with RepositoryComponent {
      val _repo = stub[Repository]
      override def repository: Repository = {
        _repo
      }
    }

    (replicator.repository.shouldSave _).when(record).returns(true)
    (replicator.repository.findRecord _).when(Array("123", "foo")).returns(0L)

    replicator.consumer.write(Array("123", "foo"), record)

    (replicator.repository.insertRecord _).verify(record)
  }
4

1 回答 1

2

def repository: Repository这是一个每次调用它时都会返回一个新存根的方法。尝试val改为在每次调用/访问时返回相同的存根:

test("A record should be inserted if the audit flag is 'I' and no existing record is found") {
  val replicator = new DbReplicatorComponent with RepositoryComponent {
    override val repository: Repository = stub[Repository]
  }
  // ...
}

此外,您使用 Array 作为参数类型。请注意,Array("foo") != Array("foo")在 scala 中,因此我建议您在使用 Array ( ) 的调用中使用argThat匹配器- 请参见此处:Unable to create stub with Array argument in ScalMockwhenfindRecord

或者尝试谓词匹配,如下所述:http: //scalamock.org/user-guide/matching/

于 2017-07-12T16:39:02.057 回答