Git Product home page Git Product logo

Comments (3)

pledbrook avatar pledbrook commented on September 26, 2024
  1. Yes, Grails 2.3 now defaults to creating Spock tests
  2. I'll need to look into this. I could be the test method name or something else.

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.

from graina2.

tikeswar avatar tikeswar commented on September 26, 2024

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!

from graina2.

pledbrook avatar pledbrook commented on September 26, 2024

Chapter has now been updated to use Spock tests, with a mention that Grails 2.2 and earlier automatically create JUnit tests.

from graina2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.