Skip to content

Instantly share code, notes, and snippets.

@benek
Last active September 28, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benek/1469721 to your computer and use it in GitHub Desktop.
Save benek/1469721 to your computer and use it in GitHub Desktop.
Ejemplo de una Spec con Spock para Unit Testing
/**
* @author Javier Ramirez
* <benek@javamexico.org>
* Date: 19/06/12
* Time: 22:30
* www.javamexico.org
*/
package org.javamexico.jm2.usuarios
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import org.joda.time.DateMidnight
import org.javamexico.jm2.util.ConstraintUnitSpec
/**
* Especificaciones Spock (UT) para clase de dominio Usuario.
*/
@TestFor(Usuario)
@Mock(Usuario)
class UsuarioSpec extends ConstraintUnitSpec {
def "Se debe poder registrar un nuevo Usuario"(){
when:
new Usuario(
usuario: 'benek',
password: 'benek',
email: 'benek@javamexico.org',
nombre: 'Javier Alberto',
apellidop: 'Ramirez',
apellidom: 'Hernandez',
fechaAlta: new Date(),
fechaNacimiento: new DateMidnight(1984, 3, 20).toDate(),
ubicacion: 'Quer�taro, MX',
bio: 'Software Developer...',
imagenPerfil: '/path/to/image.png',
estatus: 1,
reputacion: 10.00,
verificado: true
).save(failOnError: true)
then:
Usuario.findByUsuario(usuario) != null
println Usuario.findByUsuario(usuario)
where:
usuario = 'benek'
}
def "Prueba de constraints en clase de dominio Usuario"(){
when:
def obj = new Usuario("$field": val)
then:
validateConstraints(obj, field, error)
where:
error | field | val
'nullable' | 'usuario' | null
'size' | 'usuario' | getSizedString(2)
'size' | 'usuario' | getSizedString(26)
'valid' | 'usuario' | getSizedString(25)
'nullable' | 'password' | null
'size' | 'password' | getSizedString(7)
'size' | 'password' | getSizedString(26)
'valid' | 'password' | getSizedString(25)
'valid' | 'nombre' | null
'valid' | 'nombre' | getSizedString(50)
'maxSize' | 'nombre' | getSizedString(51)
'valid' | 'apellidop' | null
'valid' | 'apellidop' | getSizedString(50)
'maxSize' | 'apellidop' | getSizedString(51)
'valid' | 'apellidom' | null
'valid' | 'apellidom' | getSizedString(50)
'maxSize' | 'apellidom' | getSizedString(51)
'nullable' | 'fechaNacimiento' | null
'max' | 'fechaNacimiento' | DateMidnight.now().plusDays(1).toDate() //Siguiente dia
'valid' | 'fechaNacimiento' | DateMidnight.now().minusDays(1).toDate() //Dia anterior
'valid' | 'ubicacion' | null
'valid' | 'ubicacion' | getSizedString(50)
'maxSize' | 'ubicacion' | getSizedString(51)
'valid' | 'bio' | null
'valid' | 'bio' | getSizedString(300)
'maxSize' | 'bio' | getSizedString(301)
'nullable' | 'email' | null
'blank' | 'email' | ''
'email' | 'email' | getEmail(false)
'valid' | 'email' | getEmail(true)
'valid' | 'token' | null
'nullable' | 'estatus' | null
'max' | 'estatus' | 51
'valid' | 'estatus' | 50
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment