So, after implementing casperOne's answer, I ran into another error:
I was presented with an IOException
stating:
"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.":"App_GlobalResources"
Scott Allen provided the reason and inherent solution to this problem.
So what I did was made a new resources file in a new folder named 'TResources' in my web project, named 'TResources' purely because it is a Resources folder that is only being created and used for Testing purposes (clever, eh?)
I then changed the properties of my ResourcesWrapper
class to return TResources.Strings.MyStringResource
rather than Resources.Strings.MyStringResource
.
NOTE: The properties in the IResources
interface must not be read-only, as when setting up the mock object, if the property is read-only it will fail as the value cannot be set.
Therefore, IResources
should look a little something like this:
public interface IResources
{
string MyStringResource { get; set; }
}
ResourcesWrapper
should then implement IResources
like this:
public class ResourcesWrapper : IResources
{
public string MyStringResource
{
get
{
return TResources.Strings.MyStringResource;
}
set
{
//do nothing
}
}
}
So that you can then achieve a successful mock in your Unit Test, like this:
var mock = new Mock<IResources>();
mock.SetupProperty(m => m.MyStringResource, "");
NOTE: You don't have to specify anything in the initialValue
parameter of the method above, as the property will be returning a value retrieved from the Strings.resx
.
That concludes my question, I hope this can be helpful to someone else on internet land!
IOException
:"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.":"App_GlobalResources"
Can you provide any further tips? – Intermix