I am trying to use MoQ and System.IO.Abstraction to mock File.OpenRead function..
Sample:
using System;
using System.IO;
using System.IO.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
public class FileServiceTests
{
[TestMethod]
public void OpenRead_ShouldThrowIOException_ThenReturnValidStream()
{
// Arrange
var filePath = "testfile.txt";
var expectedContent = "Hello, World!";
var fileBytes = System.Text.Encoding.UTF8.GetBytes(expectedContent);
var memoryStream = new MemoryStream(fileBytes);
var mockFileSystem = new Mock<IFileSystem>();
var mockFile = new Mock<IFile>();
mockFileSystem.Setup(fs => fs.File).Returns(mockFile.Object);
mockFile.Setup(f => f.OpenRead(filePath))
.Returns(memoryStream);
var fileService = new FileService(mockFileSystem.Object);
using var resultStream = fileService.OpenFile(filePath);
using var reader = new StreamReader(resultStream);
var resultContent = reader.ReadToEnd();
// Assert
Assert.AreEqual(expectedContent, resultContent);
}
}
// Example service that uses IFileSystem for OpenRead
public class FileService
{
private readonly IFileSystem _fileSystem;
public FileService(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
public Stream OpenFile(string path)
{
return _fileSystem.File.OpenRead(path);
}
}
Issue:
mockFile.Setup(f => f.OpenRead(filePath)) .Returns(memoryStream);
Here I am trying to pass instance of mocked FileStream or MemoryStream.. But, I get an error when I start the test.. The OpenRead expects FileSystemStream here in "Returns".
Not sure how I can solve this..
I am trying to use MoQ and System.IO.Abstraction to mock File.OpenRead function..
Sample:
using System;
using System.IO;
using System.IO.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
public class FileServiceTests
{
[TestMethod]
public void OpenRead_ShouldThrowIOException_ThenReturnValidStream()
{
// Arrange
var filePath = "testfile.txt";
var expectedContent = "Hello, World!";
var fileBytes = System.Text.Encoding.UTF8.GetBytes(expectedContent);
var memoryStream = new MemoryStream(fileBytes);
var mockFileSystem = new Mock<IFileSystem>();
var mockFile = new Mock<IFile>();
mockFileSystem.Setup(fs => fs.File).Returns(mockFile.Object);
mockFile.Setup(f => f.OpenRead(filePath))
.Returns(memoryStream);
var fileService = new FileService(mockFileSystem.Object);
using var resultStream = fileService.OpenFile(filePath);
using var reader = new StreamReader(resultStream);
var resultContent = reader.ReadToEnd();
// Assert
Assert.AreEqual(expectedContent, resultContent);
}
}
// Example service that uses IFileSystem for OpenRead
public class FileService
{
private readonly IFileSystem _fileSystem;
public FileService(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
public Stream OpenFile(string path)
{
return _fileSystem.File.OpenRead(path);
}
}
Issue:
mockFile.Setup(f => f.OpenRead(filePath)) .Returns(memoryStream);
Here I am trying to pass instance of mocked FileStream or MemoryStream.. But, I get an error when I start the test.. The OpenRead expects FileSystemStream here in "Returns".
Not sure how I can solve this..
Share asked Mar 7 at 13:52 njdotnetdevnjdotnetdev 156 bronze badges 1 |1 Answer
Reset to default 0var fileSystem = new System.IO.Abstractions.FileSystem();
// Mock System.IO.Abstractions's FileSystem
var mockFS = new Mock<FileSystem>();
var stream = fileSystem.File.Create("my file path.txt");
mockFS.Setup(x => x.File.OpenRead("my file path.txt")).Returns(stream);
Found this answer posted by Super Jade (A million thanks)
How do I create a FileSystemStream?
However, when I test the production code the instance is null. My file does exist at the filePath..
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744925946a4601436.html
IFile.OpenRead
isn't declared to returnStream
, it's declared to returnFileSystemStream
, so that's why your mock isn't working... – Jon Skeet Commented Mar 7 at 14:04