Skip to content

Instantly share code, notes, and snippets.

@SpOOnman
Created August 16, 2012 13:22
Show Gist options
  • Save SpOOnman/3370053 to your computer and use it in GitHub Desktop.
Save SpOOnman/3370053 to your computer and use it in GitHub Desktop.
How to user mocks in controller tests
| Failure: show should redirect to index if TwitterError is thrown(pl.refaktor.twitter.TwitterControllerSpec)
| pl.refaktor.twitter.TwitterError
at pl.refaktor.twitter.TwitterControllerSpec.show should redirect to index if TwitterError is thrown_closure1(TwitterControllerSpec.groovy:29)
Tweet readTweet(String id) throws TwitterError
1 * twitterReaderServiceMock.readTweet('1') >> { throw new TwitterError() }
class TwitterController {
def twitterReaderService
def index() {
}
def show() {
Tweet tweet
try {
tweet = twitterReaderService.readTweet(params.id)
} catch (TwitterError e) {
log.error(e)
flash.message = 'There was an error on fetching your tweet'
redirect(action: 'index')
return
}
if (tweet == null) {
flash.message = 'Tweet not found'
redirect(action: 'index')
return
}
[tweet: tweet]
}
}
class TwitterController {
def twitterReaderService
def index() {
}
def show() {
Tweet tweet = twitterReaderService.readTweet(params.id)
if (tweet == null) {
flash.message = 'Tweet not found'
redirect(action: 'index')
return
}
[tweet: tweet]
}
}
class TwitterController {
def twitterReaderService
def index() {
}
def show() {
Tweet tweet
try {
tweet = twitterReaderService.readTweet(params.id)
} catch (TwitterError e) {
log.error(e)
flash.message = 'There was an error on fetching your tweet'
redirect(action: 'index')
return
}
[tweet: tweet]
}
}
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(TwitterController)
class TwitterControllerSpec extends Specification {
TwitterReaderService twitterReaderServiceMock = Mock(TwitterReaderService)
def setup() {
controller.twitterReaderService = twitterReaderServiceMock
}
def "show should redirect to index if TwitterError is thrown"() {
given:
controller.params.id = '1'
when:
controller.show()
then:
1 * twitterReaderServiceMock.readTweet('1') >> { throw new TwitterError() }
0 * _._
flash.message == 'There was an error on fetching your tweet'
response.redirectUrl == '/twitter/index'
}
def "show should inform about not found tweet"() {
given:
controller.params.id = '1'
when:
controller.show()
then:
1 * twitterReaderServiceMock.readTweet('1') >> null
0 * _._
flash.message == 'Tweet not found'
response.redirectUrl == '/twitter/index'
}
def "show should show found tweet"() {
given:
controller.params.id = '1'
when:
controller.show()
then:
1 * twitterReaderServiceMock.readTweet('1') >> new Tweet()
0 * _._
flash.message == null
response.status == 200
}
}
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(TwitterController)
class TwitterControllerSpec extends Specification {
TwitterReaderService twitterReaderServiceMock = Mock(TwitterReaderService)
def setup() {
controller.twitterReaderService = twitterReaderServiceMock
}
}
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(TwitterController)
class TwitterControllerSpec extends Specification {
TwitterReaderService twitterReaderServiceMock = Mock(TwitterReaderService)
def setup() {
controller.twitterReaderService = twitterReaderServiceMock
}
def "show should redirect to index if TwitterError is thrown"() {
given:
controller.params.id = '1'
when:
controller.show()
then:
1 * twitterReaderServiceMock.readTweet('1') >> { throw new TwitterError() }
0 * _._
flash.message == 'There was an error on fetching your tweet'
response.redirectUrl == '/twitter/index'
}
}
class TwitterReaderService {
Tweet readTweet(String id) throws TwitterError {
try {
String jsonBody = callTwitter(id)
Tweet parsedTweet = parseBody(jsonBody)
return parsedTweet
} catch (Throwable t) {
throw new TwitterError(t)
}
}
private String callTwitter(String id) {
// TODO: implementation
}
private Tweet parseBody(String jsonBody) {
// TODO: implementation
}
}
class Tweet {
String id
String userId
String username
String text
Date createdAt
}
class TwitterError extends RuntimeException {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment