Created
August 8, 2012 02:10
-
-
Save cholick/3291429 to your computer and use it in GitHub Desktop.
Example Spock Test
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
@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