Hi All, I'm facing difficulties in Silverlight (inbrowser) UnitTesting using Mock to read a file into my ViewModel.
It gives me an AccessDenied error message. Is there another alternative method for that kind of problem?
My UnitTesting is DragAndDrop Image file in Silverlight 4.
eg: unittesing.cs
var fileInfo = new Mock(); //I Can't Mock FileInfo
var fileInfo = new FileInfo("test.jpg");
Thanks Jonny, I did as follow and not working and here is my sample code snipped.
new interface class
public interface IFileInfo { string Name {get;set ;} FileStream Open(FileMode mode); }
new Wrapper Class
public class FileInfoWrapper : IFileInfo { private FileInfo fileInfo; public FileStream OpenRead() { return this.OpenRead(); } public string Name { get { return this.Name; } set { this.Name = value; } }
}
In My Test Class
[TestMethod] [Asynchronous] public void MultiFileDropTest() { list wrapperList = new list(); fileInfo.Setup(fl => fl.Name).Returns("testing.jpg");
fileInfo.Setup<Stream>(fl => fl.OpenRead()).Returns(fileStream.Object);
wrapperList .Add(fileInfo.Object);
wrapperList .Add(fileInfo.Object);
idataObject.Setup(p => p.GetData(DataFormats.FileDrop)).Returns(wrapperList .ToArray());
}
// my function (ViewModel) public BitmapImage SingleImageDropTest(IDataObject iData) { ............. var files = (FileInfo[])dataObject.GetData(DataFormats.FileDrop);
...taking the first file from the files collection
FileInfo file = files[0];
if (file != null && IsImageFile(file.Extension))
{
//File read and return bitmap code which working fine } }