Учитывая, что я новичок в отношении грааля, я пытаюсь проверить заводский класс. Класс Factory находится в папке src, так как я не хочу, чтобы он был сервисом.Grails test a Factory class
class UserFactory {
static Map userDefaults = [
email : '[email protected]',
alias : 'myalias',
firstName : 'Antonio',
lastName : 'Roma',
password : 'asdhjs',
placeOfBirth : 'Milan',
placeOfResidence: 'Berlin',
dateOfBirth : new Date() - 4000
]
static User build(Map properties, Map options = [persisted: true]) {
userDefaults.putAll properties
def user = new User(userDefaults)
if (options.persisted) { user.save() }
return user
}
static Collection<User> build(Collection<Map> properties, Map options = [persisted: true]) {
properties.collect { build(it, options) }
}
}
Я хочу, чтобы проверить, если пользователи будут созданы и сохранены должным образом через GORM. Здесь интеграционный тест:
@TestMixin(DomainClassUnitTestMixin)
class UserFactoryIntegrationSpec extends Specification {
def setup() {
mockDomain(User)
}
def cleanup() {
}
void "create user"() {
when:
User user = UserFactory.build([alias: 'xx', email: '[email protected]'])
then:
User.count() == 1
user.alias == 'xx'
user.email == '[email protected]'
user.id != null
}
void "create users"() {
when:
Collection<User> users = UserFactory.build([[alias: 'xx', email: '[email protected]'], [alias: 'yy', email: '[email protected]']])
then:
users.size() == 2
users.alias == ['xx', 'yy']
users.email == ['[email protected]','[email protected]']
users.id != [null, null]
}
}
Пользователям не хранятся, я думаю GORM не загружен правильно ... мне не хватает некоторых аннотаций ?! здесь результаты интеграции тест-приложений: UserFactoryIntegrationSpec
User.count() == 1
| |
0 false
users.id != [null, null]
| | |
| | false
| [null, null]
[rigel.resources.User : (unsaved), rigel.resources.User : (unsaved)]
Установка вариантов сборки в статическую карту - это путь к гибели. вы будете уничтожать карту по частям (с каждым вызовом), и у вас есть состояние гонки! – cfrick
проверить результат вашего вызова 'save()' и если он является ложным, вам нужно посмотреть 'user.errors'. Или вы можете передать 'failOnErrors: true' свой вызов сохранения или' validate() 'пользователь заранее. – cfrick
спасибо @cfrick! Я не заметил ... Я исправил это! – Antonio