Spock: can an interaction defined in setup() be replaced in a test case?

后端 未结 1 867
遥遥无期
遥遥无期 2021-01-03 17:54

I\'m struggling to understand something about Spock interactions in a Groovy unit test.

I have the following types:

public interface Bar {
  public S         


        
相关标签:
1条回答
  • 2021-01-03 18:48

    That's a tricky one. As stated in the docs, interactions declared in a then-block have precedence over interactions declared earlier. However, interactions declared in a then-block are scoped to the previous when-block. (This allows to have multiple when-then pairs.) Hence your last try doesn't work, but the following will:

    def setup() {
        bar.message >> "hello"
    }
    
    def "say goodbye"() {
        when:
        def msg = foo.message
    
        then:
        bar.message >> "goodbye"
        msg == "goodbye"
    }
    

    I agree that it would be good for interactions declared in test methods to always override interactions declared in setup methods. Anyway, a good alternative to overriding interactions is for each test method to call a helper method that sets up the expected interactions for that test method.

    0 讨论(0)
提交回复
热议问题