In one of the classes method calls static method Image.FromFile. Here we have the code:
public class MyImage
{
private Image _image;
public string RelPath { get; set; }
public bool LoadImage()
{
bool result = true;
try
{
this._image = Image.FromFile("C:" + this.RelPath);
}
catch (Exception)
{
this._image = null;
result = false;
}
return result;
}
}
I need to write a test, with isolation of Image.FromFile method call, which sets the value of this._image when Image.FromFile called with ecpected parameters to my faked Bitmap object.
It seems to be something like this? but do not work:
public void LoadImageTest()
{
MlgImage target = Isolate.Fake.Instance<MlgImage>(Members.CallOriginal);
Isolate.WhenCalled(()=>target.RelPath).WillReturn("test");
Image image = new Bitmap(100, 100);
FieldInfo fieldInfo = typeof(MlgImage).GetField("_image", BindingFlags.Instance | BindingFlags.NonPublic);
var mock = MockManager.MockObject(typeof(Image))
Isolate.WhenCalled((string s) => Image.FromFile(s)).AndArgumentsMatch(s=>s.EndsWith("test")).WillReturn(image);
Isolate.Invoke.Method(target, "LoadImage");
Assert.ReferenceEquals(image, fieldInfo.GetValue(target));
}
Can someone give a suggestion?