RE: [coldbox:18425] Refactoring Unit Test and PRC vars

Since the getCollection() method is used to get both rc and prc, you would need to instruct your mock what to return based on the arguments to that method.
http://wiki.coldbox.org/wiki/MockBox.cfm#.24args()

Here is how you normally get the public and private collection:
var rc = event.getCollection();
var prc = event.getCollection(private=true);

So you would want to mock up your event object like so:

// If no args, return public

Event.$(‘getCollection’).$args().$results(“public”);
// If passing in private=true, return private collection
Event.$(‘getCollection’).$args(true).$results(“private”);

As Andrew pointed out, you don’t even need to be using the getCollection() method in your handlers since event, rc, and prc are all passed in for you since ColdBox 3. Then when you test your handler method, simply pass in the rc and prc structs you want to exist along with the event object you are already passing in.

Regarding your getcoldBoxOCM() error, you will need to mock that method into your handler as well so it returns a mock CacheBox provider which a mock “get” method.

provider = mb.createEmptyMock(“coldbox.system.cache.providers.CFColdBoxProvider”).$(‘get’).$results(‘whatever’);

SUT.$(‘getcoldBoxOCM’).$results(provider);

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Hi Brad,

Thanks for your email. I have created a new GIST which tries to incorporate your suggestion for the first issue:
https://gist.github.com/nolandubeau/9818b2715289ad739dad

See the lines 25/26 which I have commented out. Is this what you’re suggesting

I only half-grasp what you outlined above, and with lines 25/26 uncommented ( and line 23 commented out) I still receive the error that PRC.user_id is undefined.

I will attempt to fix the getcoldBoxOCM() error once I fix the first one :slight_smile:

Thanks again!

Nolan

Well, you will need to replace the “public” and “private” bits with an actual struct that you want the rc and prc to be. I just put those strings in for the sake of the example.

So it would actually be more of something like this:

Event.$(‘getCollection’).$args().$results({key= “value”, key2=“value”});
Event.$(‘getCollection’).$args(true).$results({key= “value”, key2=“value”});

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 Brad. Apologies for my newbie Mocking confusion.

I have updated the GIST - https://gist.github.com/nolandubeau/9818b2715289ad739dad with what you advised, however when I run the test I still get the same error stating that “Element User_ID is undefined in PRC”.

I’m at a loss on this one. Grrr. Any ideas?

Thanks.

Nolan

SUT.getUser(Event);

What does the code in getUser() look like and how are you retrieving the prc? Are you getting it like so:

var prc = Event.getCollection(private=true);

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

I just went back and re-read your original Gist. I didn’t think you had refactored any of your code to use prc yet, I was assuming you would add var prc = event.getCollection(private=true) at that time to match how you currently were accessing the rc.

I now realize you are already accessing the prc here:
rc.user = userService.getUser(**prc.user_id,**getcoldBoxOCM().get(‘activePartners’));

Even though you haven’t updated your method signature, ColdBox is pass rc and prc in to your handler actions. If that is how you are going to access prc (I would access rc AND prc both this way if it were me), then the mocking strategy I showed you would be unnecessary. Simply pass in rc and prc into the method invocation to mimic how ColdBox works.

So sorry for the confusion. There’s basically two ways for you to access rc and prc and it seems you’re using a mix of both which has gotten our wires crossed.

To review, if you are going to get rc or prc out of the event object, you must mock the appropriate getCollection() call. However, if you are going to simply reference the rc and prc arguments passed into each handler action (recommended) then you simply need to pass your desired rc and prc structs into the method call when you unit test it. Once you decide on a standard method of rc and prc access for your code and then unit test accordingly.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Hi Brad,

Sorry for the confusion. I forgot to update the original GIST when Andrew pointed out that I wasn’t calling var prc = event.getCollection(private=true);

Here is the update GIST with both the getUser and the UserTest - https://gist.github.com/nolandubeau/9818b2715289ad739dad

I changed both the test and the function to reference Event,rc, and prc as arguments.

Is it necessary to reference arguments.prc in the function. I know CF searches the scopes if you just reference prc, but just thought I would check to see how you do it and if referencing arguments. is any faster?

Thanks.

N

It is not necessary to specify arguments. I usually omit it for brevity. Just watch out, Adobe CF and Railo differ in their scope lookup order. I forget which is what, but one favors local and another favors arguments. ironically, Railo followed Adobe’s documentation and Adobe does NOT do what they documented. As such, Railo refuses to change since they’d rather match ACF’s docs than ACF’s actual behavior :slight_smile:

So, does the final Gist work or error? it seems like it would be correct.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Good to know thanks!. I was able to get the tests to work correctly. Thanks again!

Nolan