views:

41

answers:

1

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 } }

A: 

From the code you've written I'm guessing you're trying to use the moq framework, which uses the syntax

var fileInfo = new Mock<Interface>();

You need to supply a type, so that the framework has a clue of what behaviour to expect.

in this case, you won't be able to substitute Interface with FileInfo, since FileInfo is a concrete class. Your alternatives are:

  1. find an abstract class or interface that the FileInfo class implements, that has the methods you need to use, and when using the variable on your view, declare it as that type

  2. (more likely) make a class which wraps the Fileinfo class, and an interface which it implements, which includes the method(s) you need, and declare your variable on your view as that interface type.

Jonny Cundall