Ignoring properties in a matcher with Future of a set of results

24 views
Skip to first unread message

Anatoly Rabinovich

unread,
Jan 12, 2016, 3:56:36 AM1/12/16
to specs2-users
Hi,

I have the following this matcher:
def haveBodyWith[T: TypeTag: Unmarshaller](content: T)(implicit await: Duration): Matcher[Future[HttpResponse]] =
===(content) ^^ { (f: Future[HttpResponse]) => { Await.result(f, await).entity.as[T].right.get } }

I extract the result from the HttpResponse and then compare it to the expected content. This far, this matcher worked perfectly. Now, I have a specific use case where the content of the response is a sequence of case classes, and I want to ignore one of the properties of the case class. I can't get this work:
def haveBodyWith1(content: Foo): Matcher[HttpResponse] = {
===(content) ^^ { (_: HttpResponse).entity.as[Foo].right.get.copy(name = "") }
}

case class Foo(id: Int, name: String)

If I had just the content without the future to compare, I'd done it this way (maybe something similar with a Seq of case classes), but the future make it a little more difficult.
What do you think?

Thanks!

etorreborre

unread,
Jan 12, 2016, 4:49:03 AM1/12/16
to specs2-users
Hi Anatoly,

If you look at the `Future` tab of the "out-of the box matchers" you should see that you can transform a Matcher[T] to a Matcher[Future[T]] by appending "await".

So if you have a Matcher[HttpResponse]:

response must haveBodyWith1(foo1).await

should work.

You need to make sure however that you have a proper "org.specs2.concurrent.ExecutionEnv" implicit in scope.
See here for how to get one.

Eric.

Anatoly R

unread,
Jan 12, 2016, 6:44:48 AM1/12/16
to specs2...@googlegroups.com
Hi, 

Thanks for the reply. But I'm already familiar with 'await', I wanted to avoid using it and tuck everything nicely into a single matcher. Is there a way of doing that?

Thanks again!

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

etorreborre

unread,
Jan 12, 2016, 7:25:51 AM1/12/16
to specs2-users
Wouldn't that work?

def haveBodyWith1(content: Foo): Matcher[Future[HttpResponse]] = {

===(content) ^^ { (_: HttpResponse).entity.as[Foo].right.get.copy(name = "") }
}.await
To unsubscribe from this group and all its topics, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Anatoly R

unread,
Jan 12, 2016, 8:37:27 AM1/12/16
to specs2...@googlegroups.com
No, when I use '^^^' (adept) I get an error telling "Type mismatch, expected: (Foo) => Foo, actual: (HttpResponse) => Foo".

To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

etorreborre

unread,
Jan 12, 2016, 5:08:54 PM1/12/16
to specs2-users
This works for me:

import org.specs2.concurrent.ExecutionEnv
import scala.concurrent._
import org.specs2.matcher.{FutureMatchers, Matcher}

class FuturesSpec(implicit ee: ExecutionEnv) extends mutable.Specification with FutureMatchers {
"test" >> {
callApi must haveBodyWith1(Foo(""))

}

def haveBodyWith1(content: Foo): Matcher[Future[HttpResponse]] = {
    ===(content) ^^ { (_: HttpResponse).foo.copy(name = "") }
}.await

def callApi: Future[HttpResponse] =
future(HttpResponse(Foo("foo")))(ee.executionContext)

case class Foo(name: String)

case class HttpResponse(foo: Foo)

}

Can you use `^^` instead of `^^^`?
To unsubscribe from this group and all its topics, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Anatoly R

unread,
Jan 13, 2016, 3:15:01 AM1/13/16
to specs2...@googlegroups.com
I need to ignore one of the properties of my case class (which contains 7). So, I can either use '^^^' to ignore one or two properties or use '^^' and check each needed property separately. Is there any other way of ignoring some properties of a class that I don't know about?

This works for me:

To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

etorreborre

unread,
Jan 13, 2016, 5:13:29 AM1/13/16
to specs2-users
Is this better?

def haveBodyWith1(content: Foo): Matcher[Future[HttpResponse]] = {
  ===(normalize(content)) ^^ { (response: HttpResponse) => normalize(response.foo) }
}.await

def normalize(foo: Foo) = foo.copy(name = "")

E.
This works for me:

To unsubscribe from this group and all its topics, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Anatoly R

unread,
Jan 13, 2016, 7:03:31 AM1/13/16
to specs2...@googlegroups.com
Yes, it's a solution. I just was hoping for something more built into specs2.

Thanks for the fast replies!

Is this better?

This works for me:

To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Vēnī, vīdī, vīcī (Julius Caesar)

--
You received this message because you are subscribed to a topic in the Google Groups "specs2-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specs2-users/slc7bYB_ozE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specs2-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages