Created
January 16, 2014 01:15
-
-
Save drstevens/8448048 to your computer and use it in GitHub Desktop.
Attempt to use defaultReturn to automatically set up return values to methods on mock which the return instance of the mock.
See my question to specs2-user mailing list - https://groups.google.com/forum/#!topic/specs2-users/My86VN-NT80
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//scala 2.10.2 | |
//"org.scalaz" %% "scalaz-core" % "7.0.3" | |
//"org.specs2" %% "specs2" % "2.1.1" % "test" | |
//"org.mockito" % "mockito-all" % "1.9.0" % "test" | |
import scalaz.State._ | |
import org.specs2.mutable.Specification | |
import org.specs2.mock._ | |
trait Builder { | |
def setA(a: Int): Builder | |
def setB(b: Int): Builder | |
// .. | |
def setZ(z: Int): Builder | |
} | |
case class Request( | |
a: Option[Int], | |
b: Option[Int], | |
// .. | |
z: Option[Int]) { | |
private def m = modify[Builder] _ | |
def prepare: Builder => Builder = (for { | |
_ <- m(s => a.fold(s)(s.setA)) | |
_ <- m(s => b.fold(s)(s.setB)) | |
// .. | |
_ <- m(s => z.fold(s)(s.setZ)) | |
} yield {}).exec | |
} | |
//val validRequest: Gen[Request] = ... | |
class FooSpec extends Specification with Mockito { // with ScalaCheck | |
"prepare sets the appropriate properties" ! { //Prop.forAll(validRequest) | |
val req = Request(Some(10), None, Some(11)) | |
// val builder: Builder = mock[Builder].defaultReturn(builder) // forward reference | |
// val builder: Builder = mock[Builder].defaultAnswer(_ => builder) // forward reference | |
// val tempBuilder = mock[Builder] | |
// val builder = tempBuilder.defaultReturn(tempBuilder) // NullInsteadOfMockException | |
// Must set up each call... | |
val builder = mock[Builder] | |
builder.setA(any[Int]) returns builder | |
builder.setB(any[Int]) returns builder | |
// .. | |
builder.setZ(any[Int]) returns builder | |
// Run test | |
val result = req.prepare(builder) | |
// Note that this produces UnfinishedVerificationException | |
// there was req.b.fold(no(builder).setB(any[Int]))(one(result).setB) | |
there was req.a.fold(no(builder).setA(any[Int]))(i => one(result).setA(i)) | |
there was req.b.fold(no(builder).setB(any[Int]))(i => one(result).setB(i)) | |
// ... | |
there was req.z.fold(no(builder).setZ(any[Int]))(i => one(result).setZ(i)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment