Skip to content

Instantly share code, notes, and snippets.

@elpete
Last active October 16, 2019 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpete/9d8c2c6b7999e0d7b6241e4c76479e4b to your computer and use it in GitHub Desktop.
Save elpete/9d8c2c6b7999e0d7b6241e4c76479e4b to your computer and use it in GitHub Desktop.
Swaps out WireBox mappings for corresponding mocks during a callback.
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 );
} );
}
}
}
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