Skip to content

Instantly share code, notes, and snippets.

@cholick
Created August 8, 2012 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cholick/3291429 to your computer and use it in GitHub Desktop.
Save cholick/3291429 to your computer and use it in GitHub Desktop.
Example Spock Test
@Slf4j
class RatingServiceIntSpec extends BaseDatabaseIntSpec {
RatingService ratingService
def setup() {
ratingService = injector.getInstance(RatingService.class)
}
def 'Test that save ratings persists and update'() {
given: 'Database starts empty of ratings'
assert 0 == em.createQuery('select count(*) from Rating').singleResult
scaffoldUsers()
scaffoldMovies()
when: '2 ratings are saved'
ratingService.saveRatings([
new Rating([ratingId: new RatingId([user: user1, movie: movie1]), positive: true]),
new Rating([ratingId: new RatingId([user: user1, movie: movie2]), positive: false])
])
then: 'The database contains 2 ratings'
2 == em.createQuery('select count(*) from Rating').singleResult
when: 'A rating is updated'
ratingService.saveRatings(
[new Rating([ratingId: new RatingId([user: user1, movie: movie1]), positive: false])]
)
Rating rating = em.find(Rating.class, new RatingId([user: user1, movie: movie1]))
then: 'The database still only contains 2 ratings'
2 == em.createQuery('select count(*) from Rating').singleResult
and: 'The update is reflected'
null != rating.positive
!rating.positive
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment