Closed tikeswar closed 10 years ago
BTW, you will also need to remove the Spock plugin from the dependencies in grails-app/conf/BuildConfig.groovy
if you are using Grails 2.3. Spock is included in Grails itself now.
Thank you again for the quick feedback, Peter. I think I managed to dig a solution from the internet.
Instead of using assertEquals
, I used expect:
and and:
, and it seems to work fine now. Meaning, now if I run grails test-app QuoteServiceSpec
, in the command prompt I get | Completed 1 unit test, 0 failed in 0m 1s
. Note that earlier I was getting | Completed 0 unit test, 0 failed in 0m 1s
. I also verified by opening /qotd/target/test-reports/html/index.html in a web browser and it says "A single test executed without a single error or failure!".
I am including my modified QuoteServiceSpec.groovy
class for reference. Note the only changes made in the testStaticQuoteReturnsQuicheQuote
method, and everything else the same.
package qotd import grails.test.mixin.TestFor import spock.lang.Specification /** * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions */ @TestFor(QuoteService) class QuoteServiceSpec extends Specification { def setup() { } def cleanup() { } void "test something"() { } void testStaticQuoteReturnsQuicheQuote(){ Quote staticQuote = service.getStaticQuote() //assertEquals("Anonymous", staticQuote.author) //assertEquals("Real Programmers Don't eat quiche", //staticQuote.content) expect: staticQuote.author == "Anonymous" and: staticQuote.content == "Real Programmers Don't eat quiche" } }
Sorry, I am not experienced enough yet to explain why "assertEquals" does not work and "expect:" works, but here are a few reference I Googled that were helpful ... http://stackoverflow.com/questions/18999343/how-do-you-test-service-or-controller-methods-in-grails-2-3 https://code.google.com/p/spock/wiki/SpockBasics http://jira.grails.org/browse/GRAILS-10538
Also, on a side note to "BTW, you will also need to remove the Spock plugin from the dependencies in grails-app/conf/BuildConfig.groovy
if you are using Grails 2.3. Spock is included in Grails itself now.". In my case the BuildConfig.groovy
was automatically generated by Grails and the Spoke plugin was not included to start with (therefore I did not have to remove the plugin). Thank you!
Chapter has now been updated to use Spock tests, with a mention that Grails 2.2 and earlier automatically create JUnit tests.
When I run
grails create-*
, instead of generating*Tests.groovy
(as mentioned in the book), it generates*Spec.groovy
. Below are the examples.Example 1: Pg. 10
grails create-controller quote
creates| Created file test/unit/qotd/QuoteControllerSpec.groovy
instead of (as mentioned in the book)| Created file test/unit/qotd/QuoteControllerTests.groovy
Example 2: Pg. 17
grails create-domain-class quote
creates| Created file test/unit/qotd/QuoteSpec.groovy
instead of (as mentioned in the book)| Created file test/unit/qotd/QuoteTests.groovy
Example 3: Pg. 25
grails create-service quote
creates| Created file test/unit/qotd/QuoteServiceSpec.groovy
instead of| Created file test/unit/qotd/QuoteServiceTests.groovy
Anyways, I added a method called
testStaticQuoteReturnsQuicheQuote
in myQuoteServiceSpec.groovy
class as follows, and then rangrails test-app QuoteServiceSpec
and it did NOT run any tests (in the command prompt I got| Completed 0 unit test, 0 failed in 0m 1s
).Here is my
QuoteServiceSpec.groovy
class.My questions:
Thank you in advance for any suggestions.