Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 浏览器中的Silverlight单元测试模拟<;FileInfo>;_C#_Unit Testing_Silverlight 4.0_Mocking - Fatal编程技术网

C# 浏览器中的Silverlight单元测试模拟<;FileInfo>;

C# 浏览器中的Silverlight单元测试模拟<;FileInfo>;,c#,unit-testing,silverlight-4.0,mocking,C#,Unit Testing,Silverlight 4.0,Mocking,在Silverlight(inbrowser)单元测试中,使用Mock将文件读取到我的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. 例如: Unittessing.cs

在Silverlight(inbrowser)单元测试中,使用Mock将文件读取到我的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.
例如: Unittessing.cs

var fileInfo=new Mock()//我不能模仿文件信息

var fileInfo=newfileinfo(“test.jpg”)


谢谢Jonny,我做了如下操作,没有工作,这是我的示例代码

新接口类

公共接口IFileInfo{string Name{get;set;}文件流打开(文件模式);}

新包装器类

公共类FileInfoWrapper:IFileInfo{private FileInfo FileInfo;public FileStream OpenRead(){返回this.OpenRead();}公共字符串名称{获取{返回this.Name;}设置{this.Name=value;}

}

在我的测试课上

[TestMethod][Asynchronous]public void MultiFileDropTest(){list wrapperList=new list();fileInfo.Setup(fl=>fl.Name)。返回(“testing.jpg”)

//文件读取和返回位图代码,工作正常
}}

根据您编写的代码,我猜您正在尝试使用moq框架,它使用语法

var fileInfo = new Mock<Interface>();
var fileInfo=new Mock();
您需要提供一个类型,以便框架能够了解预期的行为

在这种情况下,您将无法用FileInfo替换接口,因为FileInfo是一个具体的类。你的选择是:

  • 查找FileInfo类实现的抽象类或接口,该类或接口具有您需要使用的方法,并且在视图上使用变量时,将其声明为该类型

  • (更可能)创建一个封装Fileinfo类的类和一个它实现的接口,其中包括所需的方法,并在视图中将变量声明为该接口类型


  • 它以什么方式不起作用?它编译吗?测试是否失败或错误通过?您的测试似乎设置了一些模拟对象,但似乎没有测试任何东西。我仍然停留在该代码的中间,所以我正在寻找一些方法。
            ...taking the first file from the files collection
            FileInfo file = files[0];
    
            if (file != null && IsImageFile(file.Extension))
            {
    
    var fileInfo = new Mock<Interface>();