Testing with calls to Application level functions

So I have a public function in Application Scope, which I call from a large number of pages and cfcs, like this.
result = application.myFunction(data);

So when I am creating test for subjects that make this call, how can I mock this application level function.
I have tried creating a mockApplication and setting application=mockApplication but it does not work.

I can debug and dump the mockApplication and it is correct but the application variable is not equal to the mockApplication.

How do I mock an application level function?

@powertoaster That’s the issue with globals and global scopes like the application scope, they are not easily testable.

In your test suite, you could overwrite application.myFunction() with a mock.

application.myFunction = someMockFunction;

although what I would recommend instead is to move these functions into an object that you could test.

// GlobalMethods.cfc
component {
    function myFunction( data) {

    }
}

You could then inject this component where it’s needed and easily mock it using TestBox and MockBox.

Hope this helps :slight_smile:

Yes actually it did help.

I just created a function in the test class myFunction() then set application.myFunction = myfundtion;

That worked great, I actually do test my application classes and the methods in them, this class does not need to perform those test it just needs to not be in the way.

If I wanted to actually mock it I suppose that it would have to be part of an object, since there does not appear to be a way to mock a method of the test class itself.

@powertoaster Could you share a code snippet of your test suite? That would help me understand your test setup. Thanks!