@@ -256,7 +256,7 @@ <h2 class="text-3xl md:text-4xl font-bold text-center mb-12 text-gray-100" data-
256256 See How Readable Tests Can Be
257257 </ h2 >
258258 < div class ="max-w-3xl mx-auto " data-aos ="fade-up " data-aos-delay ="100 ">
259- < pre > < code class ="language-groovy "> < span class ="comment "> // CalculatorSpec.groovy</ span >
259+ < pre > < code class ="language-groovy "> < span class ="comment "> // CalculatorSpec.groovy</ span >
260260< span class ="keyword "> import</ span > spock.lang.Specification
261261
262262< span class ="keyword "> class</ span > < span class ="class-name "> CalculatorSpec</ span > < span class ="keyword "> extends</ span > Specification {
@@ -265,11 +265,8 @@ <h2 class="text-3xl md:text-4xl font-bold text-center mb-12 text-gray-100" data-
265265 < span class ="keyword "> given:</ span > < span class ="comment "> "A calculator instance"</ span >
266266 < span class ="keyword "> def</ span > calculator = < span class ="keyword "> new</ span > < span class ="class-name "> Calculator</ span > ()
267267
268- < span class ="keyword "> when:</ span > < span class ="comment "> "Perform the calculation"</ span >
269- < span class ="keyword "> int</ span > result = calculator.calculate(a, b, operation)
270-
271- < span class ="keyword "> then:</ span > < span class ="comment "> "The result should match the expected value"</ span >
272- result == expectedResult
268+ < span class ="keyword "> expect:</ span > < span class ="comment "> "The calculation should match the expected value"</ span > < span class ="comment "> // Changed from when/then</ span >
269+ calculator.calculate(a, b, operation) == expectedResult
273270
274271 < span class ="keyword "> where:</ span > < span class ="comment "> "Define test data"</ span >
275272 a | b | operation || expectedResult
@@ -281,6 +278,7 @@ <h2 class="text-3xl md:text-4xl font-bold text-center mb-12 text-gray-100" data-
281278 < span class ="number "> 1</ span > | < span class ="number "> -1</ span > | < span class ="string "> "-"</ span > || < span class ="number "> 2</ span >
282279 < span class ="number "> -2</ span > | < span class ="number "> -2</ span > | < span class ="string "> "*"</ span > || < span class ="number "> 4</ span >
283280 < span class ="number "> -4</ span > | < span class ="number "> -2</ span > | < span class ="string "> "/"</ span > || < span class ="number "> 2</ span >
281+ < span class ="number "> 1</ span > | < span class ="number "> 1</ span > | < span class ="string "> "+"</ span > || < span class ="number "> 42</ span > < span class ="comment "> // A failing test case</ span >
284282 }
285283
286284 < span class ="keyword "> def</ span > < span class ="string "> "Test calculate method with division by zero"</ span > () {
@@ -338,12 +336,41 @@ <h2 class="text-3xl md:text-4xl font-bold text-center mb-12 text-gray-100" data-
338336 < span class ="keyword "> throw</ span > < span class ="keyword "> new</ span > < span class ="class-name "> IllegalArgumentException</ span > (< span class ="string "> "Invalid operation: "</ span > + operation)
339337 }
340338 }
339+
340+ < span class ="class-name "> String</ span > toString() {
341+ < span class ="keyword "> return</ span > < span class ="string "> "Calc"</ span >
342+ }
341343}
342344</ code > </ pre >
343345 < p class ="text-center text-gray-400 mt-6 " data-aos ="fade-up " data-aos-delay ="200 ">
344346 Spock's clear < code class ="inline-code "> given:</ code > , < code class ="inline-code "> when:</ code > , < code class ="inline-code "> then:</ code > blocks make understanding test logic intuitive.
345347 </ p >
346- < div class ="flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 mt-8 " data-aos ="fade-up " data-aos-delay ="200 ">
348+ < div class ="mt-12 " data-aos ="fade-up " data-aos-delay ="500 ">
349+ < h3 class ="text-xl font-semibold text-center mb-4 text-gray-100 "> Example Test Output:</ h3 >
350+ < pre > ╷
351+ └─ Spock ✔
352+ └─ CalculatorSpec ✔
353+ ├─ Test calculate method: #a #operation #b = #expectedResult ✔
354+ │ ├─ Test calculate method: 1 + 2 = 3 ✔
355+ │ ├─ Test calculate method: 5 - 3 = 2 ✔
356+ │ ├─ Test calculate method: 4 * 2 = 8 ✔
357+ │ ├─ Test calculate method: 10 / 2 = 5 ✔
358+ │ ├─ Test calculate method: -1 + 1 = 0 ✔
359+ │ ├─ Test calculate method: 1 - -1 = 2 ✔
360+ │ ├─ Test calculate method: -2 * -2 = 4 ✔
361+ │ ├─ Test calculate method: -4 / -2 = 2 ✔
362+ │ └─ Test calculate method: 1 + 1 = 42 ✘ Condition not satisfied:
363+ │
364+ │ calculator.calculate(a, b, operation) == expectedResult
365+ │ | | | | | | |
366+ │ Calc 2 1 1 + | 42
367+ │ false
368+ ├─ Test calculate method with division by zero ✔
369+ └─ Test calculate method with invalid operation ✔</ pre >
370+ </ div >
371+ </ div >
372+ </ div >
373+ < div class ="flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 mt-8 " data-aos ="fade-up " data-aos-delay ="800 ">
347374 < a href ="https://groovyconsole.dev/?g=groovy_4_0&gist=437e9026ff86d2d709c2c56eb7e2eef1#g8qcDpYAQE " target ="_blank " class ="cta-button cta-primary ">
348375 Try it online
349376 </ a >
0 commit comments