[ColdBox 3.7.1] Mockbox, logbox, and Injector question

I’m still working at wrapping my head around mockbox and have made lots of progress. I have it working except when I add the logbox injector to the model that I’m testing. Everything falls apart and I’m not sure of the best way to put it back together.

Specifically I’m trying to inject logbox to help the debugging process.

I see to options but I’m not sure how to get either to work. Option 1 being the preferred option.

  1. Made the injected logbox methods work as expected.
  2. Mock the logbox functions such as logger.info(), etc… so that they at least fail gracefully.
    Thank you for any guidance.

My set is below.

Aaron

permissionTest.cfc

component extends=“coldbox.system.testing.BaseModelTest” model=“dotpath.model.permission”{

void function setup(){
super.setup();
model.init();

}

function is_manager() {
//Should return false.

assertFalse( model.is_manager( ‘Username’) );

}

}

permission.cfc

component singleton{
property name=“logger” inject=“logbox:logger:{this}”;

permission function init(){
return this;
}

function is_manager( string username ) {

logger.info('model.permission.is_manager(#username) ’ );

}

}

When unit testing you must mock all dependencies of the method you are testing. That means you want to provide that component with a logbox property with a fake info method that does nothing. In fact, ensuring the info method was called can be one of your tests.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

And a quick followup-- I haven’t tested this-- but creating the mock and injecting it will looking something like this in your test setup:

// Create empty mock of a logger and give it a dummy into() method.
mockLogger = createEmptyMock(“coldbox.system.logging.logger”).$(“info”)
// Inject the mock into the model being tested.
model.$property(propertyName=“logger”,mock=mockLogger);

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks. That helps clear things up.

The following code works.

mockLogger = getMockBox().createMock(‘coldbox.system.logging.Logger’);
mockLogger.$(“info”).$(“debug”).$(“warn”).$(“error”);
model.$property(propertyName=“logger”, mock=mockLogger);

Aaron