Built-In Assertions

Version 13 by Marc Esher
on Nov 12, 2011 19:56.

compared with
Current by Marc Esher
on Nov 12, 2011 19:58.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (44)

View page history
{toc}
h1. Core Built-in Assertions

This document outlines the core of unit testing, i.e. Assertions. Assertions are simple: You assert something, and if that assertion does not result in your expectation, the test fails. All code AFTER a failed assertion will NOT execute. Repeat: A failed assertion means the end of that test.

h32. assertTrue(boolean condition \[,String message\])

assertTrue is one of the two "bread and butter" assertions in any testing framework. The philosophy is simple: you assert something to be true; if it's not, the test fails
<cfset assertTrue( StructIsEmpty(obj.getPrivileges()), "clearPrivileges Should Have Emptied The Structure of privileges but didn't" )>
</cffunction>
{code}
{code}assertTrue has an opposite, assertFalse:
 
h32. assertFalse(boolean condition \[, String message\])\*

{code:title=assertFalse}<cffunction name="clearPrivilegesShouldClearPrivilegesFromObject">

{code}
h2. assert(boolean condition \[,String message\])
 
h3. assert(boolean condition \[,String message\])
assert is semantically equivalent to assertTrue, providing a shorter form :
</cffunction>
{code}
h2. assertEquals*(any expected, any actual \[, String message\])
 
h3. assertEquals*(any expected, any actual \[, String message\])
assertEquals is the other core assertion. Here, you're asserting that some result equals some expected result. This could be two numbers, two strings, two structs, whatever. For now, MXUnit follows the JUnit pattern of using a single assertEquals to compare any type of data.
</cffunction>
{code}
h3. Comparing Arrays, Queries, and Structures
 
h2. Comparing Arrays, Queries, and Structures
MXUnit will do a deep compare for arrays, queries, and structures. It is *not* fail-fast, meaning that an assertEquals( struct1, struct2 ) will compare every key in the struct, including any nested structures or arrays, accumulating mismatches. Once every key is visited, the existence of mismatches will signal an assertion failure. Array, Query, and Struct comparisons will add a Structure result into the debug output if the assertion fails, and you can view the full set of differences from there.
!CompareDialog.PNG|thumbnail!
h32. fail*(String message)

fail() is used to actively fail a test. It's useful when you're stubbing a test and you want to ensure it fails, like so:
</cffunction>
{code}
 
It's also useful when you're testing an "error path", like so:
</cffunction>
{code}
h2. failNotEquals*(any value, any value2 \[,String message\])
 
h3. failNotEquals*(any value, any value2 \[,String message\])
Used to fail when two values do not equal. This doesn't perform any assertion... it's simply a convenience method for providing a specific type of failure message
</cffunction>
{code}
h2. assertSame*(any obj1, any obj2 \[,String message\])
 
h3. assertSame*(any obj1, any obj2 \[,String message\])
Used to determine if the obj1 and obj2 refer to the same instance.
Note that arrays in Adobe ColdFusion are passed by value, so, this will always fail for arrays.
</cffunction>
{code}
h2. assertNotSame*(any obj1, any obj2 \[,String message\])
 
h3. assertNotSame*(any obj1, any obj2 \[,String message\])
Used to determine if the obj1 and obj2 _do not_ refer to the same instance.
Note that arrays in Adobe ColdFusion are passed by value, so, this will always pass for arrays.
</cffunction>
{code}
h1. MXUnit Assertion Extensions
 
h2. MXUnit Assertion Extensions
These are extensions to the base set of assertions and are specific to ColdFusion.Note, that these are quite simple, can be easily implemented using the base assertions above, but are provided for convenience.
<config-element type="assertionExtension" path="XPathAssert" autoload="true" override="false" />
{code}
h2. assertXPath*(String xpath, any data, \[String text\], \[String message\])
 
h3. assertXPath*(String xpath, any data, \[String text\], \[String message\])
Searches data using xpath. If text is specified, it tries to match the exact text to the xpath expression. Otherwise ,it returns true if any nodes are found that match the xpath expression. Note that, unlike most assertions, assertXPath returns an xml dom representation of any nodes found. This can be useful for further inspection.
</cfscript>
</cffunction>
{code}
{code}The above parses the results from urls and strings and searches the parsed results for the XPath expression.
Try it. Really, it works\!
h32. assertIsTypeOf*(component obj, String type)

Determines if obj is of type. type needs to be fully qualified.
</cffunction>
{code}
h2. assertIsXMLDoc*(any xml \[, String message\])
 
h3. assertIsXMLDoc*(any xml \[, String message\])
Determines if xml is a valid XML DOM object.
{code:title=assertIsXMLDoc()}<cffunction name="demoAssertIsXMLDoc">
</cffunction>
{code}
h2. assertIsArray*(any obj1)
 
h3. assertIsArray*(any obj1)
Determines if the obj1 is a valid array.
{code:title=assertIsArray()}<cffunction name="demoAssertIsArray">
</cffunction>
{code}
h2. assertIsDefined*(any obj1)
 
h3. assertIsDefined*(any obj1)
Determines if the obj1 has been defined in the available scope.
{code:title=assertIsDefined()}<cffunction name="demoAssertIsDefined">
</cffunction>
{code}
h2. assertIsEmpty*(any obj1)
 
h3. assertIsEmpty*(any obj1)
Determines if the obj1 is a 0 length string or NULL equivalent.
{code:title=assertIsEmpty()}<cffunction name="demoAssertIsEmpty">
</cffunction>
{code}
h2. assertIsEmptyArray*(any obj1,\[String message\])
 
h3. assertIsEmptyArray*(any obj1,\[String message\])
Determines if the obj1 is an array with no elements.
{code:title=assertIsEmptyArray()}<cffunction name="demoAssertIsEmptyArray">
</cffunction>
{code}
h2. assertIsEmptyQuery*(any obj1,\[String message\])
 
h3. assertIsEmptyQuery*(any obj1,\[String message\])
Determines if the obj1 is a query with no rows.
{code:title=assertIsEmptyQuery()}<cffunction name="demoAssertIsEmptyQuery">
</cffunction>
{code}
h2. assertIsEmptyStruct*(any obj1,\[String message\])
 
h3. assertIsEmptyStruct*(any obj1,\[String message\])
Determines if the obj1 is a struct with no keys or values.
{code:title=assertIsEmptyStruct()}<cffunction name="demoAssertIsEmptyStruct">
</cffunction>
{code}
h2. assertIsQuery*(any q)
 
h3. assertIsQuery*(any q)
Determines if q is a valid ColdFusion query.
{code:title=assertIsQuery()}<cffunction name="demoAssertIsQuery">
</cffunction>
{code}
h2. assertIsStruct*(any obj)
 
h3. assertIsStruct*(any obj)
Determines if obj is a valid ColdFusion structure.
{code:title=assertIsStruct()}<cffunction name="demoAssertIsStruct">