How does one mock a function (not a method) with M...
# testing
a
How does one mock a function (not a method) with MockBox. IE: a function that's just in a library .cfm file instead of a method in a .cfc:
Copy code
<cfscript>
// MyCoolLibrary.cfm


function testMe() {
    if (avoidRunningMe()) {
        return "one fish"
    }
    return "two fish"

}

function avoidRunningMe() {
    // really slow or is destructive or has external dependencies or something
}
</cfscript>
The library is just included when one wants to use it, eg like this:
Copy code
describe("Testing MyCoolLibrary", () => {
    include "MyCoolLibrary.cfm"
    it("checks testMe does the right thing", () => {
        // now I need to mock avoidRunningMe before calling testMe...?
    })
})
(full disclosure: I have not looked at the mocking code within MockBox/TestBox to try to dig out an answer, and my eyeballing of the docs was superficial. I'm not asking anyone to do this for me, I'm just wondering if anyone already knows the answer. If there is one).
d
I'd be 99% sure that it isn't supported, as mockbox works by decorating CFC instances. In this case, you'd need to turn your test case itself into a mocked instance. Bit of a headF* A util UDF lib testing CFC would do the trick (pass it a udf path, or more than one, have it include the UDFs). Get an instance, turn it into a mocked object and go. (I'm sure you've thought of something similar, or perhaps there is something in Mockbox/Testbox already)
a
Cheers @domwatson , yeah that's how I'd tackle it if I needed to hand-crank it. Was just checking whether there was a pre-canned version of same. It's actually not something I need, but someone asked me today, and I didn't know the answer so I thought I'd find out. In the end it was a brain fart on the part of the person concerned as despite the function we needed to mock being in a .cfm file (this is CFWheels "architecture" in play), the .cfm was being included into a .cfc file anyhow, and the context we where testing for was in one of those CFCs in the first place. So there was actually no issue to address. If you see what I mean. But I like to know the answers to these things ๐Ÿ™‚
I wonder what would happen if one did this:
Copy code
import test.BaseSpec

component extends=BaseSpec {

	function run() {

		describe("Testing MyCoolLibrary", () => {
            include "MyCoolLibrary.cfm"
			it("checks testMe does the right thing", () => {
                prepareMock(this) // <---------------
                this.$("avoidRunningMe").$results("whatevs")
                
                result = testMe()
                
                // etc
            
			})
		})
	}
}
d
๐Ÿ’ฅ๐Ÿงจ๐Ÿ„ โ˜๏ธ
(or it will just work ;))
a
hahaha
Well!
I'm on Lucee, so dunno whether this behaviour is legit. But as spec'ed above: no it does not work. However if I hoist the include to be outside the
describe
block... either in the
run
or before it... yes it does work.
So:
Copy code
import test.BaseSpec

component extends=BaseSpec {

    include "MyCoolLibrary.cfm"

	function run() {

		describe("Testing MyCoolLibrary", () => {
			it("checks testMe does the right thing", () => {
                prepareMock(this)
                this.$("avoidRunningMe").$results("whatevs")
                
                result = testMe()
                
                // etc
            
			})
		})
	}
}