apache / apache/grails-core

beforeUpdate not working for domain unit test?

Open
#14,561 0 comments 0 reactions 1 assignee Claimed by @puneetbehl View on GitHub
relates-to: gorm relates-to: grails-testing
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Task List

- [x] Steps to reproduce provided
- [x] Stacktrace (if present) provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)

### Steps to Reproduce

Here is my Domain class: `grails-app/domain/mygrailsproj/MyDomain.groovy`

```groovy
package mygrailsproj

class MyDomain {

String field

def beforeUpdate() {
return false
}
}
```

Here is my unit test: `src/test/groovy/mygrailsproj/MyDomainSpec.groovy`:

```groovy
package mygrailsproj

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification

class MyDomainSpec extends Specification implements DomainUnitTest {
void "did not expect domain could be modified"() {
setup:
MyDomain myDomain = new MyDomain(field: 'old').save(flush: true)

when:
myDomain.field = 'new'
myDomain.save(flush: true)
myDomain.refresh()

then:
myDomain.field == 'old'
}
}
```

### Expected Behaviour

Domain will not be modified if `beforeUpdate()` returns false.

### Actual Behaviour

Test failed:

```
did not expect domain could be modified
Condition not satisfied:

myDomain.field == 'old'
| | |
| new false
| 3 differences (0% similarity)
| (new)
| (old)
mygrailsproj.MyDomain : 1

at mygrailsproj.MyDomainSpec.did not expect domain could be modified(MyDomainSpec.groovy:17)
```

### Environment Information

- **Operating System**: Linxmint 19
- **GORM Version:** 6.1.10.RELEASE
- **Grails Version (if using Grails):** 3.3.6
- **JDK Version:** 1.8.0_172

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.