One small detail I can't get to be perfect yet: ``...
# spek
v
One small detail I can't get to be perfect yet:
Copy code
on("1122") {
    val input = "1122"
    val result = taskA(input)

    it("should produce 3") {
        assertEquals(3, result)
    }
}
As you see, there are unfortunate repetitions of the input value and the assert value if I want to use them in the section names. How do you tackle that?
The second repetition of the expected value ii not so bad:
Copy code
on("1122") {
    val input = "1122"
    val result = taskA(input)

    val expect = 3
    it("should produce $expect") {
        assertEquals(expect, result)
    }
}
But I can't do that for the input value, can I?
t
The argument to
on(...)
is a description of what the specification is confirming. "1122" is a bit less verbose than what a description should be (maybe not with context, and just my opinion, take that with a grain of salt). Technically, you could extract that
1122
to a variable before the
on
block like this:
Copy code
val firstTestvalue = "1122"
on(firstTestvalue) {
    val result = taskA(firstTestvalue)
...
However, I don't think that does anything for readability of the test. the way you have it now looks better IMO.
Your original comment is perfectly fine, I think. You could just remove the
val input = "1122"
and call
val result = taskA("1122")
without losing any clarity.
v
@twbarber the problem with extracting a part of the description in a local val is that I cant repeat that pattern for several
on{}
sections:
Copy code
given{
  val input = ...
  on("such $input" ) {...}

  val input = ...  // name clash or an ugly inconsistency :disappointed: 
  on("such $input" ) {...}
t
Right, and I don't think you should. I think this is preferable:
Copy code
on("1122") {
    val result = taskA("1122")

    it("should produce 3") {
        assertEquals(3, result)
    }
}
This looks fine IMO. OR skip the on and just go
Copy code
given("...") {
    it("should return 3 when the input is 1122") {
        assertEquals(3, taskA("1122"))
    }
    it("should return X when the input is Y") {
        assertEquals(X, taskA("Y"))
    }
    ...
}
v
WOW, it can skip
on{}
??? That's very cool, will try it! Thanks!
👍 1