I am currently testing an akka application.
I have come across a certain pattern: I want to test that a TestProbe
has received a certain message, modulo some fields.
For example, if the message was:
UserInfo(username: String, score: Int, timestamp: String)
Then I might want to test the username
and score
are as expected, but not care at what time the message was received.
Currently I would like write something like this:
testProbe.expectMsgPF() {
case UserInfo(`username`, `score`, _) =>
}
How could the test probe class be extended so that something like this might be written instead?
testProbe.expectApproxMsg(UserInfo(`username`, `score`, _))
In addition to shortening my code, I'm hoping an answer to this question will further my understanding of the Scala programming language.
I think you can not do something like this
Because, first last attribute (timestamp) isn't a
var
is aval
needs to have value and if what you want is passing a pattern by parameter you can't either 'cause we can not pass pattern alone without allcase
alternatives (PartialFunction).So
UserInfo(
username,
score, _)
is an object, a normal instance.But we can do a workaround extending
TestProbe
class and adding a default value to last UserInfo's attribute class.Look at the following, maybe it works for you:
HelloSpec.scala
Source code of Testkit