Details
Description
mockDomain() doesn't mock dirty checking related methods in domain classes. These include:
isDirty()
getPersistentValue()
getDirtyPropertyNames()
Workaround is to test anything that requires these in an integration test, but this is sub-optimal IMHO.
-
Hide
- grails-7506-bug-report-05072012.zip
- 05/Jul/12 3:59 AM
- 22 kB
- Raviteja
-
- grails-app/.../ApplicationResources.groovy 0.1 kB
- grails-app/conf/BootStrap.groovy 0.1 kB
- grails-app/conf/BuildConfig.groovy 2 kB
- grails-app/conf/Config.groovy 4 kB
- grails-app/conf/DataSource.groovy 1 kB
- grails-app/conf/UrlMappings.groovy 0.2 kB
- grails-app/conf/spring/resources.groovy 0.0 kB
- grails-app/domain/org/.../domain/Test.groovy 0.1 kB
- grails-app/i18n/messages.properties 3 kB
- grails-app/.../messages_cs_CZ.properties 3 kB
- grails-app/i18n/messages_da.properties 3 kB
- grails-app/i18n/messages_de.properties 4 kB
- grails-app/i18n/messages_es.properties 3 kB
- grails-app/i18n/messages_fr.properties 2 kB
- grails-app/i18n/messages_it.properties 2 kB
- grails-app/i18n/messages_ja.properties 4 kB
- grails-app/i18n/messages_nl.properties 3 kB
- grails-app/.../messages_pt_BR.properties 3 kB
- grails-app/.../messages_pt_PT.properties 3 kB
- grails-app/i18n/messages_ru.properties 4 kB
- grails-app/i18n/messages_sv.properties 3 kB
- grails-app/i18n/messages_th.properties 6 kB
- grails-app/.../messages_zh_CN.properties 2 kB
- grails-app/views/error.gsp 0.3 kB
- grails-app/views/index.gsp 3 kB
- grails-app/views/layouts/main.gsp 2 kB
- test/unit/org/.../domain/TestTests.groovy 0.8 kB
- application.properties 0.1 kB
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
A workaround is to dynamically add the method yourself in your unit tests
YourDomain.metaClass.isDirty =
{ //do functionality here }it is non-trivial to implement dirty check tracking in a unit test. We could add methods that simply return false in the short term, but a broader implementation is more complex. In the meantime is is not that complicated to use the mocking API to mock the methods.
Graeme, Ravi,
i agree mocking is the right path.
however, to achieve it on plain domain instances saved and retrieved by the test implementations of save() and findBy(), i feel like i must do partial mocking : overwriting the method through the metaclass.
it's not that elegant in the test code; we especially have to restore the former method at the end...
what do you think of offering a "setDirty" (i let you find the correct name) in the test implementation of domain classes mixins, such as for save() and findBy() ?
thank you for your opinion
This is still a problem in Grails 2 when using @Mock or @TestFor