Last active
October 16, 2019 15:55
-
-
Save elpete/9d8c2c6b7999e0d7b6241e4c76479e4b to your computer and use it in GitHub Desktop.
Swaps out WireBox mappings for corresponding mocks during a callback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
/** | |
* Swaps out WireBox mappings for corresponding mocks during a callback. | |
* mappings = { "apiClient" = mockApiClient } | |
*/ | |
function whileSwapped( struct mappings = {}, any callback, boolean verifyMappingExists = true ) { | |
var binder = getWireBox().getBinder(); | |
var originalMappings = {}; | |
mappings.each( function( mapping, component ) { | |
var mappingExists = binder.mappingExists( mapping ); | |
if ( verifyMappingExists && ! mappingExists ) { | |
expect( mappingExists ).toBeTrue( "No #mapping# already configured in WireBox" ); | |
} | |
if ( mappingExists ) { | |
originalMappings[ mapping ] = binder.getMapping( mapping ); | |
} | |
binder.map( alias = mapping, force = true ).toValue( component ); | |
} ); | |
try { | |
callback(); | |
} catch ( any e ) { | |
rethrow; | |
} finally { | |
originalMappings.each( function( mapping, component ) { | |
binder.setMapping( mapping, component ); | |
} ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component extends="tests.resources.BaseIntegrationSpec" { | |
function run() { | |
describe( "Currency API", function() { | |
it( "can convert an amount from one currency to another", function() { | |
var currencyApiStub = createStub() | |
.$( "getExchangeRate" ) | |
.$args( "EUR", "USD" ) | |
.$results( 1.11 ); | |
whileSwapped( { "currencyAPI": currencyApiStub }, function() { | |
var event = get( "/currency/convert", { | |
"amount": 10.00, | |
"fromCurrency": "EUR", | |
"toCurrency": "USD" | |
} ); | |
expect( event.getStatusCode() ).toBe( 200 ); | |
expect( event.getRenderData().data ).toBe( 11.10 ); | |
} ); | |
} ); | |
} ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment