chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
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?
asked by atikhomirov (1.6k points)

1 Answer

0 votes
I'm sorry but it seems that the text you wrote is unreadable - please try to repost or send an email to support AT typemock DOT com
answered by dhelper (11.9k points)
...