Grails 2.0 Testing Modules Custom Lib Tag

I have a custom lib tag that I wrote so that I can easily display object properties. It allows me to call

<g:property label="Name" property="${user.name}"/> 

Which keeps my views short and consistent, and also allows me to make changes quickly. My taglib code is as follows:

 def property = {attrs, body -> def startingHtml = "..." def endingHtml = "..." out << startingHtml renderField(attrs.property) out << endingHtml } def renderField(property) { if (property instanceof Collection) { property.each { out << it + "</br>" } } else if(property instanceof Address){ renderAddress(property) } else { out << property } } def renderAddress(address) { out << "Address Render Logic Here" } 

I am trying to add some unit tests around this code because it has logic in it. Using the examples given here ( http://grails.org/doc/latest/guide/testing.html#unitTestingTagLibraries ), I started adding some tests. The first two scripts that are processed by my tag are String and Collection, which I could check correctly (the first two tests below). The final script I need to test is an Address object (which is only a POGO with string attributes). I cannot find a way to check if the object is passed as an attribute to the lib tag.

 @TestFor(PropertyTagLib) class PropertyTagLibTests { @Test void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAString() { String result = applyTemplate('<g:property label="something" property="someTextValue"/>').trim() assert result.contains('someTextValue') } @Test void propertyTagShouldRenderList() { String result = applyTemplate('g:property label="something" property="[\"one\", \"two\", \"three\"]"/>').trim() assert result.contains("one</br>two</br>three</br>") } @Test void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAnAddress() { def address = new Address(line1: "Line 1") String result = applyTemplate('<g:property label="something" property="${address}"/>').trim() assert result.contains("Address Render Logic Here") } } 

How can I check my taglib when one of the attributes is an object?

+4
source share
2 answers

So I solved this by “cutting out the middle person” and bypassing applyTemplate() , and directly going to the method that executes the logic. This is not entirely ideal for two reasons: 1) I do not claim that the property tag is connected using renderField, but hopefully my other tests, which still use applyTemplate() , guarantee this. 2) In the real world, out in TagLib is the creator of the output, and in my test I created it as a list (I can do this, since all you can do is left shift will suffice).

What I like about using the list is that it helps to maintain order.

 @Test void renderFieldShouldRenderAddress() { def address = new Address(line1: "Line 1", line2: "Line 2", line3: "Line 3", city: "City", state: "ST", zipCode: "12345", country: "USA", buildingNumber: null, buildingName: null, roomNumber: null ) def tagLib = new PropertyTagLib() def results = [] tagLib.metaClass.getOut = { results } tagLib.renderField(address) assert "Line 1" == results[0] assert "<br />" == results[1] assert "Line 2" == results[2] assert "<br />" == results[3] assert "Line 3" == results[4] assert "<br />" == results[5] assert "City, ST 12345" == results[6] assert "<br />" == results[7] assert "USA" == results[8] assert "<br />" == results[9] assert 10 == results.size() } 

Any thoughts?

0
source

The applyTemplate () declaration is as follows:

 String applyTemplate(String contents, Map model = [:]) 

Tag parameters are passed through model .

Your test might look like this:

 @Test void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAnAddress() { String result = applyTemplate('<g:property label="something" property="${address}"/>', [address : new Address(line1: "Line 1")]).trim() assert result.contains("Address Render Logic Here") } 
+7
source

Source: https://habr.com/ru/post/1393863/


All Articles