Skip to content

Commit

Permalink
Changes to support MightyMock integration into MXUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
bobsilverberg committed Nov 28, 2009
1 parent d8417ea commit 0efea63
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 15 deletions.
35 changes: 23 additions & 12 deletions MightyMock.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,35 @@
};

if( arguments.size() eq 1){
mocked.name = arguments[1];
return this;
if (isObject(arguments[1])) {
return createTypeSafeMock(arguments[1]);
} else {
mocked.name = arguments[1];
return this;
}
}

/*
Make multiple type safe mocks.
Make a type safe mock.
*/
if( arguments.size() eq 2 ) {
try{
return createMultipleTypeSafeMocks(arguments[1]);
}
catch (coldfusion.runtime.CfJspPage$NoSuchTemplateException e){
_$throw('InvalidMockException',e.getMessage(),e.getDetail());
}
return createTypeSafeMock(arguments[1]);
}
}


//Clears all methods in object to be mocked.
function createMultipleTypeSafeMocks(name){
var proxy = createObject('component', name);
mocked.name = name;
function createTypeSafeMock(mockee){
var proxy = 0;
try{
if (not IsObject(mockee)) {
proxy = createObject('component', mockee);
mocked.name = mockee;
} else {
proxy = mockee;
mocked.name = getMetaData(mockee).name;
}

proxy.snif = _$snif; //sniffer for variables scope
proxyVars = proxy.snif();

Expand Down Expand Up @@ -131,6 +138,10 @@
proxy.variables._$DUMP = _$DUMP;

return proxy;
}
catch (coldfusion.runtime.CfJspPage$NoSuchTemplateException e){
_$throw('InvalidMockException',e.getMessage(),e.getDetail());
}

}

Expand Down
25 changes: 25 additions & 0 deletions MockFactory.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<cfcomponent displayname="MockFactory" output="false">

<cffunction name="init">
<cfscript>
return this;
</cfscript>
</cffunction>

<cffunction name="createMock">
<cfargument name="mocked" required="false" default="" />
<cfargument name="mockType" required="false" default="fast" />
<cfswitch expression="#arguments.mockType#">
<cfcase value="fast">
<cfreturn createObject("component","MightyMock").init(arguments.mocked) />
</cfcase>
<cfcase value="typeSafe">
<cfreturn createObject("component","MightyMock").init(arguments.mocked,true) />
</cfcase>
<cfcase value="partial">
<cfthrow type="MightyMock.MockFactory.partialMocksNotImplemented" message="Partial mocks are not available via MightyMock yet." />
</cfcase>
</cfswitch>
</cffunction>

</cfcomponent>
37 changes: 37 additions & 0 deletions test/MockFactoryTest.cfc
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<cfcomponent output="false" extends="BaseTest">

<cffunction name="createPartialShouldThrow" mxunit:expectedException="MightyMock.MockFactory.partialMocksNotImplemented">
<cfset mock = mf.createMock("mxunit.framework.TestCase","partial") />
</cffunction>


<cfscript>

/* Bill's tests for MightyMockFactory.cfc
Now using this test component to test MockFactory.cfc used for MXUnit integration
function testMockCreation(){
mock1 = $('foo');
assertIsTypeOf(mock1,'foo');
Expand All @@ -9,9 +18,37 @@ function testMockCreation(){
debug(mock2);
assertIsTypeOf(mock2,'WEB-INF.cftags.component');
}
*/

function createNoNameNoTypeShouldCreateNamelessFastMock() {
mock = mf.createMock();
assertIsTypeOf(mock,"MightyMock.MightyMock");
assertEquals("",mock.getMocked().name);
}


function createNameedNoTypeShouldCreateNamedFastMock() {
mock = mf.createMock("foo");
assertIsTypeOf(mock,"MightyMock.MightyMock");
assertEquals("foo",mock.getMocked().name);
}

function createTypeSafeShouldCreateTypeSafeMock() {
mock = mf.createMock("mxunit.framework.TestCase","typeSafe");
assertIsTypeOf(mock,"mxunit.framework.TestCase");
assertEquals("mxunit.framework.TestCase",mock.getMocked().name);
}

function createWithActualObjectShouldCreateTypeSafeMock() {
testCase = createObject("component","mxunit.framework.TestCase");
mock = mf.createMock(testCase);
assertIsTypeOf(mock,"mxunit.framework.TestCase");
assertEquals("mxunit.framework.TestCase",mock.getMocked().name);
}

function setUp(){

mf = createObject("component","MightyMock.MockFactory");

}

Expand Down
17 changes: 14 additions & 3 deletions test/ReturnTypeTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ function testThatInitIsCallingCreateTypeSafeMocks(){

}

function testCreateMultiplTypeSafeMocks(){
mymock = createObject('component','mightymock.MightyMock').createMultipleTypeSafeMocks(dummy);
mymock2 = createObject('component','mightymock.MightyMock').createMultipleTypeSafeMocks(mockery);
function testCreateTypeSafeMock(){
mymock = createObject('component','mightymock.MightyMock').createTypeSafeMock(dummy);
mymock2 = createObject('component','mightymock.MightyMock').createTypeSafeMock(mockery);
assertIsTypeOf(mymock, 'mightymock.test.fixture.Dummy');
assertIsTypeOf(mymock2, 'mightymock.test.fixture.Mockery');
assertIsTypeOf(mock, 'mightymock.test.fixture.Dummy');

}

function testCreateTypeSafeMockWithObject(){
dummy = createObject('component',dummy);
mockery = createObject('component',mockery);
mymock = createObject('component','mightymock.MightyMock').createTypeSafeMock(dummy);
mymock2 = createObject('component','mightymock.MightyMock').createTypeSafeMock(mockery);
assertIsTypeOf(mymock, 'mightymock.test.fixture.Dummy');
assertIsTypeOf(mymock2, 'mightymock.test.fixture.Mockery');
assertIsTypeOf(mock, 'mightymock.test.fixture.Dummy');
Expand Down

0 comments on commit 0efea63

Please sign in to comment.