Created
August 16, 2012 13:22
-
-
Save SpOOnman/3370053 to your computer and use it in GitHub Desktop.
How to user mocks in controller tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tweet readTweet(String id) throws TwitterError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 * twitterReaderServiceMock.readTweet('1') >> { throw new TwitterError() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import grails.test.mixin.TestFor | |
import spock.lang.Specification | |
@TestFor(TwitterController) | |
class TwitterControllerSpec extends Specification { | |
TwitterReaderService twitterReaderServiceMock = Mock(TwitterReaderService) | |
def setup() { | |
controller.twitterReaderService = twitterReaderServiceMock | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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