Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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# 如何在应用程序MVC moq中删除moq文件(IFileSystem)_C#_Asp.net Mvc_Moq - Fatal编程技术网

C# 如何在应用程序MVC moq中删除moq文件(IFileSystem)

C# 如何在应用程序MVC moq中删除moq文件(IFileSystem),c#,asp.net-mvc,moq,C#,Asp.net Mvc,Moq,我的工作方法是什么 [HttpPost] public async Task<ActionResult> DeleteTeam(int id) { Team team = await teamRepository.DeleteTeamAsync(id); var fileToDeletePath = Path.Combine(Server.MapPath("~/Images/NBAlogoImg/"), team.Path);

我的工作方法是什么

[HttpPost]
    public async Task<ActionResult> DeleteTeam(int id)
    {
        Team team = await teamRepository.DeleteTeamAsync(id);
        var fileToDeletePath = Path.Combine(Server.MapPath("~/Images/NBAlogoImg/"), team.Path);

        if (System.IO.File.Exists(fileToDeletePath))
        {
            System.IO.File.Delete(fileToDeletePath);
        }
        if (team != null)
        {
            TempData["message"] = string.Format("{0} был удален", team.Name);
        }
        return RedirectToAction("Index", "Player");
    }
[HttpPost]
公共异步任务DeleteTeam(int-id)
{
Team Team=await teamRepository.DeleteTeamAsync(id);
var fileToDeletePath=Path.Combine(Server.MapPath(“~/Images/NBAlogoImg/”),team.Path);
if(System.IO.File.Exists(fileToDeletePath))
{
System.IO.File.Delete(filetodeletpath);
}
如果(团队!=null)
{
TempData[“message”]=string.Format(“{0}бббббб”,team.Name);
}
返回重定向到操作(“索引”、“播放器”);
}
这是我试图做的测试,但没有成功

 [TestMethod]
    public async Task CanDeletePlayerAsync()
    {
        //Arrange
        Mock<ITeamRepository> teamsMock = new Mock<ITeamRepository>();

        Team team2 = new Team { Id = 2, Name = "Boston" , Path = "CHi.png" };
        Team team3 = new Team { Id = 3, Name = "Lakers" };

        string fullPath = ("~/Images/NBAlogoImg/");

        var serverMock = new Mock<HttpServerUtilityBase>();
        serverMock.Setup(x => x.MapPath(fullPath)).Returns(@"s:\work");

        var httpContextMock = new Mock<HttpContextBase>();
        httpContextMock.Setup(x => x.Server).Returns(serverMock.Object);

        var mockFile = new Mock<IFileSystem>();

        TeamController controller = new TeamController(teamsMock.Object);
        controller.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), controller);

        teamsMock.Setup(m => m.DeleteTeamAsync(team2.Id)).Returns(Task.FromResult(team2));

        // Act
        ActionResult result = await controller.DeleteTeam(team2.Id);
        mockFile.Verify(x => x.File.Delete(@"s:\work\file.png"));

        //Assert
        Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
    }
[TestMethod]
公共异步任务CanDeletePlayerAsync()
{
//安排
Mock teamsMock=新Mock();
teamteam2=新团队{Id=2,Name=“Boston”,Path=“CHi.png”};
teamteam3=新团队{Id=3,Name=“lakes”};
字符串完整路径=(“~/Images/NBAlogoImg/”;
var serverMock=new Mock();
Setup(x=>x.MapPath(fullPath))。返回(@“s:\work”);
var httpContextMock=new Mock();
httpContextMock.Setup(x=>x.Server).Returns(serverMock.Object);
var mockFile=new Mock();
TeamController=新的TeamController(teamsMock.Object);
controller.ControllerContext=新的ControllerContext(httpContextMock.Object,new RouteData(),controller);
Setup(m=>m.DeleteTeamAsync(team2.Id)).Returns(Task.FromResult(team2));
//表演
ActionResult=wait controller.DeleteTeam(team2.Id);
mockFile.Verify(x=>x.File.Delete(@“s:\work\File.png”);
//断言
IsInstanceOfType(result,typeof(RedirectToRouteResult));
}
如果我删除团队,我会添加从应用程序中删除图像的功能。它工作得很完美,但如何通过Moq进行测试我尝试了一些尝试,但没有成功

我收到了错误信息

预期至少对模拟进行一次调用,但从未执行过:x=>x.File.Delete(“s:\work\File.png”) 未配置任何设置。 没有执行任何调用


它是如何修复的?我已经下载了
IFileSystem
并制作了一个moq,但验证没有工作。

一个明显的解决方案是包装您的文件。例如,在实现自定义接口的自定义类中删除调用

public interface IFileOperations
{
    void Delete(string path);
}
对于您的系统操作,您可以创建一个包装器类

public class SystemFileOperations:IFileOperations
{
    public void Delete(string path)
    {
        File.Delete(path);
    }
}
现在,您可以更改原始代码,以确保在需要IFileOperations.Delete的所有位置注入SystemFileOperations

private IFileOperations _fileOperations;
public ControllerName(IFileOperations operations)
{
_fileOperations = operations;
}
随后将替换以下行

System.IO.File.Delete(fileToDeletePath);

对于嘲弄,你可以

var mock = new Mock<IFileOperations>();
mock.Verify(x=>x.Delete(path),Times.AtLeastOnce());
var mock=new mock();
验证(x=>x.Delete(path),Times.atlestOnce());

请注意,在您的情况下,由于使用了File.Exists,如果您希望,您可能还必须遵循相同的模式来模拟它。一个明显的解决方案是包装您的文件。例如,在实现自定义接口的自定义类中删除调用

public interface IFileOperations
{
    void Delete(string path);
}
对于您的系统操作,您可以创建一个包装器类

public class SystemFileOperations:IFileOperations
{
    public void Delete(string path)
    {
        File.Delete(path);
    }
}
现在,您可以更改原始代码,以确保在需要IFileOperations.Delete的所有位置注入SystemFileOperations

private IFileOperations _fileOperations;
public ControllerName(IFileOperations operations)
{
_fileOperations = operations;
}
随后将替换以下行

System.IO.File.Delete(fileToDeletePath);

对于嘲弄,你可以

var mock = new Mock<IFileOperations>();
mock.Verify(x=>x.Delete(path),Times.AtLeastOnce());
var mock=new mock();
验证(x=>x.Delete(path),Times.atlestOnce());

请注意,在您的情况下,由于使用了File.Exists,如果您愿意,您可能还必须按照相同的模式来模拟它

您没有在实际代码中使用
IFileSystem
,这就是验证失败的原因。您没有在实际代码中使用
IFileSystem
,这就是验证失败的原因。我按照你对我说的那样做了,但我至少在模拟上做了一次预期调用,但从未执行过相同的问题:x=>x.Delete(“s:\work\CHi.png”),你能分享一下你的控制器构造函数或你注入IFileOperation的地方在这部分代码
中看起来像问题吗(System.IO.File.Exists(filetodeletpath))
如果我删除了它,所有的功能都会正常工作,但我试图使“按存储库”的功能无法正常工作help@remk93您需要将它添加到接口中,一个exist方法。您还需要模拟它。我将verify更改为强设置,它将运行`SetupfileMock.Setup(x=>x.Exists(returnPath))。返回(true);fileMock.Setup(x=>x.Delete(returnPath));`我不明白为什么验证不起作用?我按照你对我说的做了验证,但我至少在模拟上调用了一次相同的问题,但从未执行过:x=>x.Delete(“s:\work\CHi.png”)如果(System.IO.File.Exists(fileToDeletePath))存在代码
if部分的问题,您能分享一下您的控制器构造函数或注入IFileOperation的位置吗
如果我将其全部删除,但我尝试按存储库进行设置,则不会起作用help@remk93您需要将它添加到接口中,一个exist方法。您还需要模拟它。我将验证更改为强设置,并且它正在工作`SetupfileMock.Setup(x=>x.Exists(returnPath))。返回(true);fileMock.Setup(x=>x.Delete(returnPath))我不明白为什么验证不起作用?