Skip to content

Instantly share code, notes, and snippets.

@ck1125
Created February 22, 2011 19:13
Show Gist options
  • Save ck1125/839183 to your computer and use it in GitHub Desktop.
Save ck1125/839183 to your computer and use it in GitHub Desktop.
Controller with multiple inter-related collaborators
package mypackage
import grails.plugin.spock.ControllerSpec
import org.hamcrest.CoreMatchers
import static org.hamcrest.CoreMatchers.notNullValue
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat
class FancyControllerTests extends ControllerSpec {
def mockService1
def mockService2
def mockService3
def setup() {
mockService1 = Mock(Service1)
mockService2 = Mock(Service2)
mockService3 = Mock(Service3)
controller.service1 = mockService1
controller.service2 = mockService2
controller.service3 = mockService3
}
def "should mock out interactions with multiple collaborators"() {
def param1 = "yada"
given: "url with param1 value yada is requested"
controller.params.param1 = param1
when:
def model = controller.index()
then: "ensure that collaborators are called as expected"
1 * mockService1.serviceCall1({it == "${param1}"}) >> mockDataProducer1(1,1,1)
1 * mockService2.serviceCall2(producer1.result.property) >> mockDataProducer2() //call to serviceCall2 is based on return value from serviceCall1
and: "model should be populated with the correct data"
assertThat("We should have content 1", model.content1, notNullValue())
assertThat("We should content2 should have value 2", model.content2, CoreMatchers.is(2))
}
def mockDataProducer1(Integer param1, Integer param2, Integer param3) {
return mockData1 //non-data base mock data
}
def mockDataProducer2(param1) {
return mockData2 //again not grails domain data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment