使用 Scalatest 测试 ScalikeJDBC Publisher

Test ScalikeJDBC Publisher with Scalatest

你知道为什么这段代码 它应该 "add person s and then find-by-firstname" in { implicit session: DBSession => val person1: Person = Person.create(firstname1, lastname1, ssn1, email1, dob1).get val person3: Person = Person.create(firstname1, lastname3, ssn3, email3, dob3).get

val publisher1: DatabasePublisher[Person] = Person.findByFirstName(firstname1)

val probe1 = TestSink.probe[Person](actorSystem)
val (_, sink) = Source
  .fromPublisher(publisher1)
  .toMat(probe1)(Keep.both)
  .run()

sink.request(2)
sink.expectNext(person1, person3)
sink.expectComplete()

}

会出现这个错误

2019-09-26 16:28:13,846 INFO  s.s.DatabasePublisher@[kka.actor.default-dispatcher-6] - Database stream requested by subscriber: akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$$anon@17b79df0 is ready
2019-09-26 16:28:13,855 INFO  s.s.DatabaseSubscription@[la-execution-context-global-24] - All data for subscriber: akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$$anon@17b79df0 has been sent
2019-09-26 16:28:13,857 INFO  s.s.DatabaseSubscription@[la-execution-context-global-24] - Finished cleaning up database resources occupied for subscriber: akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$$anon@17b79df0
[ERROR] Tests run: 11, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.683 s <<< FAILURE! - in com.bah.devops.common.entities.PersonTest
[ERROR] Person should add person s and then find-by-firstname(com.bah.devops.common.entities.PersonTest)  Time elapsed: 0.144 s  <<< FAILURE!
    java.lang.AssertionError: assertion failed: expected OnNext(Person(14,FN1,LN1,000-00-0001,1@email.test.com,2019-01-02)), found OnComplete
        at com.bah.devops.common.entities.PersonTest.$anonfun$new(PersonTest.scala:418)
        at com.bah.devops.common.entities.PersonTest.$anonfun$new$adapted(PersonTest.scala:284)
        at com.bah.devops.common.entities.PersonTest.using(PersonTest.scala:27)
        at com.bah.devops.common.entities.PersonTest.withFixture(PersonTest.scala:27)

?不知道为什么两个onNext电话好像漏接了

这样就可以了。

val person1: Person = Person.create(firstname1, lastname1, ssn1, email1, dob1).get val person3: Person = Person.create(firstname1, lastname3, ssn3, email3, dob3).get

val publisher1: DatabasePublisher[Person] = Person.findByFirstName(firstname1)

val results1Future: Future[Seq[Person]] =
  Source.fromPublisher(publisher1).take(2).runWith(Sink.seq)

val results1: Seq[Person] = Await.result(results1Future, Duration(5, TimeUnit.SECONDS))

results1 should contain(person1)
results1 should contain(person3)