Skip to content

Instantly share code, notes, and snippets.

@isummation
Last active December 31, 2018 13:53
Show Gist options
  • Select an option

  • Save isummation/dc903088e19a8e8097176004e746ebda to your computer and use it in GitHub Desktop.

Select an option

Save isummation/dc903088e19a8e8097176004e746ebda to your computer and use it in GitHub Desktop.
/**
* @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");
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment