mock a static function
Closed this issue · 1 comments
Drew
Is there a way to mock out a Singleton instance? I have the following pretty stock Flex URLParser and need to test the logic of parsing the URL.
public class MainPresenter
{
public var browserManager:IBrowserManager;
public function setIntialView():void
{
browserManager = BrowserManager.getInstance();
browserManager.init();
var object:Object = URLUtil.stringToObject(browserManager.fragment,"&");
}
Problem I hit is at the "getInstance" line, a new browserManager is set and the mock I do is cleared. I do not know if it is possible to mock static classes, specifically the BrowserManager & URLUtil classes. If I can mock them, then I can control the result.
Thanks
Des
Inject the IBrowserManager
instance to your MainPresenter instead of getting it via BrowserManager.getInstance()
. In testing inject a mocked IBrowserManager, and in your application setup inject the instance from BrowserManager.getInstance()
.
Mockolate will quite happily mock the IBrowserManager
interface. Static functions cannot be mocked, see limitations and gotchas. If you would like an explanation why, ask and I will put it up on the site.