How to return values for multiple calls that depend on arguments? #693

Closed
asarkar opened this Issue Feb 4, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@asarkar

asarkar commented Feb 4, 2017

  1. subscriber.receive(_) >> "ok" is for one call.
  2. subscriber.receive(_) >>> ["ok", "error", "error", "ok"] is for multiple calls.
  3. subscriber.receive(_) >> { args -> args[0].size() > 3 ? "ok" : "fail" } is for computing return value.

How to combine 2 and 3? The following doesn't work.

class MyFirstSpec extends Specification {
  RemoteClient client

  def setup() {
    client = Mock(RemoteClient)
    client.call(_) >>> [{args -> args[0]}] >> {args -> 1}
  }
  def "let's try this!"() {
    when:
    def result1 = client.call(1)
    def result2 = client.call(15)
    then:
    result1 == 1
    result2 == 1
  }
}
@asarkar

This comment has been minimized.

Show comment
Hide comment
@asarkar

asarkar Feb 4, 2017

OK, I found by trial-and-error that the following works. Please document this as the triple-shift with array syntax doesn't work here, neither does >>> followed by >>:

client.call(_) >> {args -> throw new RuntimeException ("${args[0]}")} >> {args -> 1}

asarkar commented Feb 4, 2017

OK, I found by trial-and-error that the following works. Please document this as the triple-shift with array syntax doesn't work here, neither does >>> followed by >>:

client.call(_) >> {args -> throw new RuntimeException ("${args[0]}")} >> {args -> 1}

@leonard84

This comment has been minimized.

Show comment
Hide comment
@leonard84

leonard84 Feb 22, 2017

Member

@asarkar please look at org.spockframework.smoke.mock.ChainedResponseGenerators in spock-specs and tell me what not works, and if you look at the interaction based testing chapter for more documentation.

Member

leonard84 commented Feb 22, 2017

@asarkar please look at org.spockframework.smoke.mock.ChainedResponseGenerators in spock-specs and tell me what not works, and if you look at the interaction based testing chapter for more documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment