Mocking static get gorm method in Grails unit test -
I am using Grails 2.3.7. I have a simple person domain object:
class person {string name fixed constraints = {}}
and controller:
@ Transactional (readonly = true) Class Person Controller {Df Index () {Render View: 'Index', Model: [Person List: Personality ())} Display DIF (Long ID) {DF Person = Person. (ID) Render view: 'Show', Model: [person: person]} ...}
I am trying to write a unit test for the controller show
method, but I'm not sure that a static call Person.get (id)
returns a value.
Here's my (unsuccessful) attempt:
import grails.test.mixin * Import spock.lang * @TestFor (Person Controller) @mock (person) class person controller espec specification {Zero "test shows expansion action involves the person provided in the id and model by the load" () {setup: def PersonMockControl = mockFor (person) PersonMockControl.demand.static.get () {int id - & gt; New person (name: "John")} when: "A valid person identity card for show action" Controller Show (123) Then: "There is a model population in which the individual ID loaded by that unique ID is the domain frequency" model.person! = Null model.person.name == "John"}}
This test fails because the condition model.person! = Null
fails. How do I re-write this exam to pass it?
Edit
In this case, I can do the steps by the Test Reicher on a statutory call:
Zero "test Show action loaded person ID and that person is included in the model "() {setup: def p = new person (name:" John ") Save () when: "Idle Show Action" passed to the controller.show (p.id) Individual ID: "There is a model population in which the individual ID loaded by that unique ID is the domain frequency" model.person! = Null model.person.id == p.id However, I really want to understand how to correctly implement person
stable ? "
Get the method. Edit
Get the
method of my alleged need to be fake Here is another condition for displaying this filter code:
DIF FITTERS = {Purchase Filters (Controller: "PayPal", Action: "Buy") {after {= New seller payment (payment: request payment, vendor: get vendor (params.buyerId)). Save ()}}}
I want to test this I'm shaking that this filter expects using the following tests:
zero "is called the PayPal plugin controller, when it is a seller to make sure to buyFilter's payment Test "() {setup: // How can I return to the seller (params.buyerId) // To save a new seller back to a seller: When with the filter (action:" purchase ") {Admin: By ()} Then: Vendor Payment DhaHPRO, like dot () == 1}
Needs do not make a person mimetic .get ()
is a favorite / proper way of grabbing something in an exam by the way you have - You create a domain example, and pass in the controller's action to consume its ID.
However, if you need to duplicate a static method like this, then there are two approaches:
1) mockFor
by GrailsUnitTestMixin Use the method provided
and then specify the static method like this:
GrailsMock mockPerson = mockFor (person) mockPerson.demand.static.get () {Long ID - & gt; Note that in your case, the exact parameter type ( does not occur) in the static fetch
method uses instead of long
) So this method was not being used. 2) Modify the square ' MetaCLASS
. Changing MetaCLASS is very discouraged (especially when @Mock
annotations sets everything for you), and can test leakage (i.e., other tests may be changed to your meta-class The latter will not work properly, unless you restore the original metalex after the exam).
He said, for acquaintance, the way you will duplicate .get ()
method:
Person.metaClass.static .get = {long id - & gt;
Again, this is considered bad behavior and should be avoided, but you have it there.
Comments
Post a Comment