spock icon indicating copy to clipboard operation
spock copied to clipboard

Improved assertion message in "with(object) {}"?

Open szpak opened this issue 4 years ago • 3 comments

Issue description

    def "should keep verbose error message"() {
        given:
            String foo = "foo message"
        expect:
            foo.contains("bar")
    }

results in very nice error message:

Condition not satisfied:

foo.contains("bar")
|   |
|   false
foo message

However, when with(object) {} is used with the same assertion:

    def "should keep verbose error message"() {
        given:
            String foo = "foo message"
        expect:
            with(foo) {
                contains("bar")
            }
    }

it looks worse:

Condition not satisfied:

contains("bar")
|
false

Maybe it would be possible in AST transformation to get the real object value and present it?

Additional Environment information

Spock 2.0-M4-groovy-3.0

Java/JDK

OpenJDK 15

Groovy version

3.0.7

szpak avatar Mar 01 '21 20:03 szpak

This might not be trivial, as you can't really decide at compile time, whether the method belongs to the delegate or the specification.

  def "spec message"() {
    given:
    String foo = "foo message"
    expect:
    with(foo) {
      cont(it)
    }
  }

  boolean cont(String str)  {
    return str.contains("bar")
  }

leonard84 avatar Mar 02 '21 18:03 leonard84

@szpak I was thinking about adding new withContext/(verifyAllContext|verifyAllWithContext)? methods, that would just add the target object to the exception message.

leonard84 avatar Jun 29 '22 17:06 leonard84

A simple workaround is to still use it.contains("bar")

leonard84 avatar Jun 29 '22 17:06 leonard84