You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Scalacheck to define objects that will be used in my unit tests.
val listingGen = for {
id <- posNum[Long]
name <- alphaStr
} yield Listing(id, name)
//I want that listingGen always return me a Listing
listingGen.sample.get
the listingGen.sample could return None. How can I force scalacheck to return me Some(Listing)?
The text was updated successfully, but these errors were encountered:
In the general case, you can't. ScalaCheck generators can fail, for instance if you're adding a filter (listingGen.suchThat(...)), and that failure is modeled with the Option type. However, if you're sure that your generator never will fail, you can simply call Option.get like you do in your example above. Or you can use Option.getOrElse to replace None with a default value.
If you know that your generator might fail, but very seldomly, you can use Gen.retryUntil(_ => true). This will simply re-run a generator if it fails. You should be careful with this, since you might end up in a non-terminating loop, though.
I'm using Scalacheck to define objects that will be used in my unit tests.
the
listingGen.sample
could returnNone
. How can I force scalacheck to return meSome(Listing)
?The text was updated successfully, but these errors were encountered: