- May 30, 2019
- Posted by: iSummation Team
- Category: Testing

There are multiple testing techniques we use for software code testing, most common are Unit Testing, Model Testing, Functional Testing, Handler Testing, System Testing, Regression Testing etc. Usually full testing to follow by writing specific test cases as depend on testing technique and tools that you use.
Testing for Coldfusion software application can be done through “TestBox” – A testing framework for ColdFusion (CFML) that provide a clean syntax to write test cases for ColdFusion based software. TestBox is an ideal testing tool for ColdBox Framework for Lucee CFML. Complete help on TestBox is available on ColdBox site.
So in this blog you will find simple test cases for unit testing and handler testing help to initiate testing with complex example.
Let us start with setup and configuration
Get TESTBOX from official website, Once downloaded extract testbox zip on root of project folder and rename with “testbox”.
Moving on further for unit / model testing.
There is a model as companyservice.cfc, now to create a model testing there is a code file for testcase called companyservicetest.cfc.
Real all lines written below to get an idea about how TestBox code and function used here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
/** * @Author Parixit * @Date 30 MAY 2016 * @use companyServiceTest **/ /* coldbox.system.testing.BaseModelTest need to extends for unit testing for below example. */ component extends="coldbox.system.testing.BaseModelTest" { <strong>beforeAll()</strong> Any code written in this function will execute at the starting of execution and will executes only once. function beforeAll(){ /* Model direcorty has companyService.cfc model file. <strong>CreateMock()</strong> is the method which create the service object with the additional testing function with service's own function. <strong>CreateEmptyMock()</strong> is the method which create the service object with only testing helper functions. This will eliminate the models own functions. <strong>PrepareMock()</strong> In case of we create the object of service with createObject function and we want testing capabilities in that object then we can use PrepareMock(<Object>) . */ companyservice = createMock(className="models.companyService"); companyGateway = createEmptyMock(className="models.companyGateway"); companyDao = createEmptyMock(className="models.companyDAO"); /* companyService.$property() is the method added by creating the mock of the companyservice. This function can assign the properties to the mockobject which are assign in companyservice model it self. */ companyService.$property(propertyname = "companyDAO", propertyScope="instance",mock=companyDAO); companyService.$property(propertyname = "companyGateway", propertyScope="instance",mock = companyGateway); /* QuerySim() function is use to create a mock of the query. With predefined data. */ nativeQuery = querySim("id,name,address,city,state,zip,country,website 1| iSummation Technologies Pvt. Ltd|304, Shapath-3 Sarkhej-Gandhinagar Road,Bodakdev|Ahmedabad|Gujarat|380054|India|www.isummation.com/"); } <strong>run()</strong> All the testcases will written in the run method. function run(){ /* <strong>describe()</strong> This describe function contains all the specifications of test case. Below I Mention the Function name "Funtion : Delete Company" describe for which function all the testcase specifications are written. <strong>it( "Specification Name",function() {} )<strong> : this is called spacs. All the testcase related logics should be in the spacs. */ describe( "Funtion : Delete Company", function(){ it( "Delete Company", function(){ companyDao.$("delete").$results(true); /* - First above statement not execute actual method from the companyDAO because COMPANYDEO is createEmptyMock so there is no method of companyDAO object is available here so we can just give the signature of function here and also predefine the result of results as well. - Above line contains two new functions call $("delete") and $results(true). - Using $() you can get the control of any function written in companyDAO model like Here I have use the "delete". - Using $results(true) this function will help you to get the predefine result i.e TRUE. - So when this statement executes then actual method will not called but it seems like delete called and we already set the execution result that is TRUE. */ var result = companyservice.deletecompany(1); /* - In above function "deletecompany" called directly because We create the mock so it contain all method of the model and the method of testcases. That's because no need to call function by $(""). */ expect( result ).toBeTypeOf("struct","Success"); /* - Now it's time to check the testing. - expect() function will check is the output of the function is as per the expectation or not. - Results may be any struct, Boolean, Array, number and so on... - toBe() the Matchers that match our expectation. There are number of matcher functions you can refer in the help. */ expect( result.success ).toBe(true); }); }); describe( "Function : Save Company", function(){ it( "Save Company", function(){ // companyGateway is the emptymock object so to call "getDataByQuery" function, needs $("") and use $results() if function return anything. companyGateway.$("getDataByQuery").$results(nativeQuery); // companyDao is the emptymock object so to call "save" function, needs $("") and use $results() if function return anything. companyDao.$("save").$results(company); var result = companyservice.savecompany(company,1); expect( result ).toBeTypeOf("struct","Success"); expect( result.success ).toBe(true); }); }); describe( "Function getcompanys", function(){ it( "get By getByAttributesQuery", function(){ companyGateway.$("getByAttributesQuery").$results(query); var result = companyservice.getcompanys(); expect( result ).toBeTypeOf("query","Success"); }); }); } } |