Hey Dojo-community
I was wondering if there is a way to test functions that do calls to one of the SRMManager helpers.
Is there a way to mock this? Or will this result in having to write the code in an entirely different way?
Just as an easy example, how to test the following function:
public Resource[] ExampleFunction()
{
return SrmManagers.ResourceManager.GetResources(ResourceExposers.Name.Equal("test"));
}
Hi Maxim,
If you want to mock data from the server, then it is advised that you mock via an interface and create separate test class of the interface.
Example:
interface ISRMHelper which has method "Resource[] GetResource(string name)
class SRMHelper : ISRMHelper. This is the class used in production code.
class TestSRMHelper: ISRMHelper. This is the test class for testing ISRMHelper.
production code could be like this:
ISRMHelper _helper;
...
public bool CheckOnResources()
{
string elementName = this.Element.Name;
var resources = _helper.GetResources(elementName);
return resources?.Length > 0;
}
Implementation of GetResources
in production: return SrmManagers.ResourceManager.GetResources...
in test: anything you want, such as returning object from a dictionary<string, Resource>
You can then write unit tests and do calls like "var hasResources = myManager.CheckOnResources();".
You can mock the outcome and check if the code works in your production code.
Hope this helps you further