Specs Presentation

2,156 views

Published on

An overview of Specs (v1.4.4) with example code.

Published in: Technology, Business
1 Comment
1 Like
Statistics
Notes
  • Get example code from http://github.com/Synesso/specs-presentation/tree/master

    Slideshare has corrupted the formatting a little. I recommend downloading the source file.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
2,156
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
18
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Specs Presentation

  1. 1. Specs _ what it is <ul><li>Scala library for Behaviour-Driven Development. </li></ul>See the associated code for a working example - HelloWorldSpec.scala import org.specs._ object HelloWorldSpec extends Specification { &quot;'hello world' has 11 characters&quot; in { &quot;hello world&quot; .size must_== 11 } &quot;'hello world' matches 'h.* w.*'&quot; in { &quot;hello world&quot; must beMatching( &quot;h.* w.*&quot; ) } } [info] Running specs tests... [info] [info] Testing scalamelb.specs.HelloWorldSpec ... [info] specifies [info] + 'hello world' has 11 characters [info] + 'hello world' matches 'h.* w.*' [info] [info] All tests PASSED.
  2. 2. Specs _ structure <ul><li>BDD builds on TDD to allow tests to be written in the business domain language.
  3. 3. The Specs structure: Specify system (with context) and examples </li><ul><li>&quot;An empty stack&quot; should &quot;be of size zero&quot; </li></ul></ul>NigerianPrinceSpec.scala &quot;A Nigerian prince with access to an email account&quot; should { &quot;contact you in relation to his most generous offer&quot; in { // exercise the logic and make assertions ... } }
  4. 4. Specs _ structure <ul><li>Examples can be nested
  5. 5. Systems can't
  6. 6. Specifications can, sort of </li></ul>SnowflakeSpec.scala &quot;A snowflake&quot; should { [info] A snowflake should &quot;be cold&quot; in { [info] + be cold &quot;and icy&quot; in { … } [info] + and icy &quot;and unique&quot; in { … } [info] + and unique &quot;A snowflake&quot; should { &quot;in the desert&quot; should { &quot;melt&quot; in { … } object SnowflakeSpec extends Specification { &quot;A snowflake&quot; isSpecifiedBy SnowflakeInTheDesertSpec , SnowflakeInTheFrideSpec , SnowflakeOnTheTongueSpec }
  7. 7. Specs _ matchers <ul><li>Matchers are the assertion mechanism. </li><ul><li>Allow natural language assertions
  8. 8. 100+ are documented at time of writing (v1.4.4)
  9. 9. Can write custom matchers. Can be combinatorial. </li></ul></ul>PhoneBillSpec.scala &quot;A phone bill&quot; should { &quot;keep a count of calls made&quot; in { bill.numberOfCallsMade must_== 3 &quot;A phone bill&quot; should { &quot;include a call to my mother&quot; in { bill.callsMade must contain (&quot;9898 4477&quot;) &quot;A phone bill&quot; should { &quot;export calls to file&quot; in { bill.exportFile must (beReadable and not(beHidden))
  10. 10. Specs _ scalacheck <ul><li>Matcher for ScalaCheck </li><ul><li>(A test case automation tool)
  11. 11. Default of 100 arbitrary test input values per statement
  12. 12. For all n (where n >=0) √n² must equal n </li></ul></ul>SquareRooter.scala object SquareRooterSpec extends Specification with ScalaCheck { &quot;A Square Rooter&quot; should { &quot;find the originally squared value (as long as it wasn't negative)&quot; in { // forAll { (n: Int) => scala.Math.sqrt(n*n) == n } must pass // fails on -1 forAll { n: Int => n >= 0 ==> (scala.Math.sqrt(n*n) == n) } must pass
  13. 13. Specs _ tear up / down <ul><li>Traditional setup & teardown mechanisms available for before and after: </li><ul><li>Specification
  14. 14. each System
  15. 15. each Example </li></ul></ul>SetupAndTeardown.scala object SetupAndTeardown extends Specification { doBeforeSpec{ println( &quot;Specification setup&quot; ) } doFirst{ println( &quot;System setup&quot; ) } doBefore{ println( &quot;Example setup&quot; ) } … doAfter{ println( &quot;example teardown&quot; ) } doLast{ println( &quot;System teardown&quot; ) } doAfterSpec{ println( &quot;Specification teardown&quot; ) } }
  16. 16. Specs _ shared contexts <ul><li>Before and after context blocks can be encapsulated in Context values and &quot;threaded&quot; into Systems.
  17. 17. They come with a warning – expect the unexpected if shared and mutated in different Systems within the Specification. And don't feed after midnight. </li></ul>ThreadedLists.scala var listOfStrings:List[ String ] = Nil val withThreeValues = beforeContext { listOfStrings = &quot;bob&quot; :: &quot;harry&quot; :: &quot;dorothy&quot; :: Nil } &quot;A list of three Strings&quot; ->-(withThreeValues) should { &quot;be of three strings long&quot; in { listOfStrings.length must_== 3 } }
  18. 18. Specs _ system contexts <ul><li>Alternatively, System Contexts can be constructed to always return a system in a given state.
  19. 19. They come in two flavours: </li><ul><li>Internal (a SystemContext instance)
  20. 20. External (a case class extending SystemContext) </li></ul></ul>MonkeyKing.scala object MonkeyKingSpec extends Specification with SystemContexts { /* Demonstrates explicit, internal system context. See example for more. */ &quot;The monkey king under attack&quot; should { val monkeyKingUnderAttack = systemContext { new MonkeyKing ( true ) } &quot;summon warriors from his ear hairs&quot; .withA(monkeyKingUnderAttack) { monkeyKing => monkeyKing.summonsWarriorsFromEarHair must beTrue
  21. 21. Specs _ mocking <ul><li>Both jMock and Mockito can be mixed-in </li><ul><li>Specs adds syntactic sugar to ease mocking </li></ul></ul>USBLightSpec.scala object USBLightSpec extends Specification with Mockito { &quot;A green USB light&quot; should { &quot;be observed but not adjusted&quot; in { val light = mock[ Light ] light.getColour returns Green val colour = LightObserver(light).observeLightsColour colour must_== Green light.getColour was called light.setIntensity _ wasnt called
  22. 22. Specs _ alternatives <ul><li>ScalaTest
  23. 23. Instinct </li></ul>class StackSpec extends Spec with ShouldMatchers { describe( &quot;A newly created Stack&quot; ) { val stack = new Stack[Any] it( &quot;should be empty&quot; ) { stack should be ('empty) } } } class ANewlyCreatedStack { val stack = new Stack[Any] @Specification def shouldBeEmpty = { expect that stack.depth isEqualTo 0 } }

×