Thursday, February 14, 2008

ScalaTest and Mocking

I've added in some mocking into ScalaTest thanks to specs integration. I've done it in a BDD style, kind of like rspec's given/when/then. rspec is much further along and I still need to learn a lot more about it, but thats okay. I'm hoping just to get some peoples opinions on readability and such. Here are a couple very quick examples.



mockTest( "Report should be generated for each invocation" ){

val reporter = mock(classOf[Reporter])

expecting( "reporter gets 10 passing reports because invocationCount=10" ) {
nTestsToPass( 10, reporter )
}

when ( "run the suite with method that has invocationCount=10" ){
new TestNGSuiteWithInvocationCount().runTestNG(reporter)
}
}

mockTest( "Reporter should be notified when test is skipped" ){

val reporter = mock(classOf[Reporter])

expecting ( "a single test should fail, followed by a single test being skipped" ){
one(reporter).runStarting(0)
one(reporter).testStarting(any(classOf[Report]))
one(reporter).testFailed(any(classOf[Report]))
one(reporter).testIgnored(any(classOf[Report]))
one(reporter).runCompleted()
}

when ( "run the suite with a test that should fail and a test that should be skipped" ){
new SuiteWithSkippedTest().runTestNG(reporter)
}
}



As you can see, I've added expecting and when blocks, which optionally take a String (that is currently just used for readability). The simple idea here is that I have grouped chunks of my test into places that make sense, hopefully making it easier for someone reading the test. Do you like it? Do you hate it? Any other opinions that could help?