Używam Grails 2.3.7. Mam proste obiektu domeny Osoba:Wyśmiewanie statycznej metody get gorm w teście jednostek Grailsa
class Person {
String name
static constraints = {}
}
i kontrolera:
@Transactional(readOnly = true)
class PersonController {
def index() {
render view:'index', model:[personList:Person.list()]
}
def show(Long id) {
def person = Person.get(id)
render view:'show', model:[person:person]
}
...
}
próbuję napisać badanej jednostki dla kontrolera do wykonywania metody show
, ale nie jestem pewien, w jaki sposób trzeba skonfigurować test, aby upewnić się, że wywołanie statyczne Person.get(id)
zwraca wartość.
Oto moje (braku) próba:
import grails.test.mixin.*
import spock.lang.*
@TestFor(PersonController)
@Mock(Person)
class PersonControllerSpec extends Specification {
void "Test show action loads Person by id and includes that person in the model rendered"() {
setup:
def personMockControl = mockFor(Person)
personMockControl.demand.static.get() { int id -> new Person(name:"John") }
when:"A valid person id passed to the show action"
controller.show(123)
then:"A model is populated containing the person domain instance loaded by that unique id"
model.person != null
model.person.name == "John"
}
}
Ten test nie powiedzie się, ponieważ warunek model.person != null
zawiedzie. Jak przerobić ten test, aby go przekazać?
EDIT
W tym przypadku mogę bok kroku statyczne wywołanie metody przez przerabianiu test wygląda następująco:
void "Test show action loads Person by id and includes that person in the model rendered"() {
setup:
def p = new Person(name:"John").save()
when:"A valid person id passed to the show action"
controller.show(p.id)
then:"A model is populated containing the person domain instance loaded by that unique id"
model.person != null
model.person.id == p.id
model.person.name == "John"
}
jednak naprawdę chciałbym, aby zrozumieć, w jaki sposób poprawnie mock Person
' s statyczna metoda get
.
EDIT 2
Oto kolejna sytuacja zademonstrować moje postrzeganą potrzebę mock metodę get
. Biorąc pod uwagę ten kod: Filtr
def fitlers = {
buyFilter(controller:"paypal", action:"buy") {
after = {
new VendorPayment(payment:request.payment, vendor: Vendor.get(params.buyerId)).save()
}
}
}
Próbuję sprawdzić, czy filtr ten działa zgodnie z oczekiwaniami za pomocą następującego testu:
void "test the buyFilter to ensure it creates a VendorPayment when the PayPal plugin controller is called"() {
setup:
// how do I mock the Vendor.get(params.buyerId)
// to return a vendor in order to save a new VendorPayment
when:
withFilters(action:"buy") {
controller.buy()
}
then:
VendorPayment.count() == 1
}
Dlaczego wymagane jest sfałszowanie metody "get"? Już wyśmiewasz "Osobę" w teście i tworzysz fałszywą instancję Osoby. Czy GORM wykonuje swoją pracę w celu pobrania wymaganej instancji Person wewnątrz "show"? Pomaga? – dmahapatro
Próbowałem ugotować prosty przykład potrzeby wyśmiewania metody 'get' i być może ten przykład był zbyt uproszczony. Będę edytować pytanie, aby dodać bardziej zaangażowaną sytuację. –