mocking - Using Spock to stub both Gorm and other methods in a Grails domain class -


Sorry if this is a newbie question but I really appreciate any insight that the community can offer me in relation to a problem location service (string target, string locator, app app, boolean sync = true) {If (! Target) to throw a new, IllegalArgumentException ("Illegal value for MSID:" + target) def locRequest = Request.create (target, Type.LOCATE) if (! LocRequest.save ()) {location location. Terror (target, "error in continuing to request location")} LocationSource.locateTarget (target, locator, app, sync)}

I have a domain class, request is there Along with the default GORM methods, there are some additional domain methods, e.g.

  @EqualsAndHashCode class request {string reference string MSID type type position status destination destination date time date sorted date time date stabilize (string msid, type type, destination destination = destination.default) ) {New request (reference: context), type: type, position: position. Valid, date-wise: new date time ())}   

Finally, I have a spock specificationLand

Ideally, I should mock both default GORM methods, but some additional domain logic, such as the method of creating a static, should validate anything.

Ideally, I use Scope but I can not use them here according to the post given below Peter N, those who call them need to be injected and in this case The request (which I am trying to fake), it has been made as a method of place in the Location Validation in the Location Valuable:

And neither do I Grails 2.x can use @Mock annotation I am unsure if it will be fake of Goram methods, I am unsure if I

Therefore, in the end, I am trying to use Groovee Stubfour / Mockfor methods because I believe To call it that will be used in the test method to close it (wrapping down) in the bottom.

Here is the test space:

  @TestFor (LocationsServices) // @ Mok (Request) extension of the class space service service specification {@Shared app = "TEST_APP" @ Share Target = "123" @ Shared locator = "999" def Location Service = New Location Service () LocationSource locationSource = Mock () Def "Goal synchronous must be detected to detect a valid target" (given: def stub = New StubFor (requested)) Stub.demand.create {target, type -> New request (msid: target, type: type)} stub.demand.save {true} 1 * locationSource.locateTarget (target, locator, app, sync) & gt; & Gt; {Location.create (target, point, cellId, lac)} def space: stub.use {location = locationService.locate (target, locator, app)} then: location}   

However, when I run the test, though the stubbed method returns my review stub object, I get a failure on the Stabd Save method:

  groovy.lang.MissingMethodException: No signature of the method: Com.domain.Request.save () applies for logic types: () Value: [] Possible solutions: Save (), save (boolean), (java.util.Map) Anyone can tell me what I am doing wrong here or the best way to solve my special case. Can recommend the way if extra methods need to be stooled, as well as domain class goram methods that I can not inject directly into the test?  

Thank you in advance,

Patrick

I believe That you should be able to use Grails' @Mock annotation entioned for goram methods, and then you will need to manually duplicate static methods: < Pre> @TestFor (LocationService) @Mock (request) // This is suggested, as it will counterfeit the goram methods class space service SEEPEC extends {... void setup () {Request.metaClass.static.create = {string Msaidi, type, destination, destination = destination. Default - & gt; // some logic here}} ...

When using @Mock annotation, Grails duplicate the default methods (saved / received / dynamic finder) But it does not do anything in any extra way you added, so you need to manually duplicate them.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -