Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace System.IO.Abstractions.TestingHelpers.Tests
{
using NUnit.Framework;
using Text;
using XFS = MockUnixSupport;
[TestFixture]
class MockFileSystemSerializationTests
{
[Test]
public void SerializationBytes()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");

var content = "Hello there!" + Environment.NewLine + "Second line!" + Environment.NewLine;
var expected = Encoding.ASCII.GetBytes(content); //Convert a C# string to a byte array

var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(XFS.Path(@"c:\something"));

fileSystem.File.WriteAllBytes(path, expected);

//Act
var memoryStream = new MemoryStream();
var serializer = new Runtime.Serialization.Formatters.Binary.BinaryFormatter();
serializer.Serialize(memoryStream, fileSystem);
memoryStream.Flush();
memoryStream.Position = 0;
fileSystem = (MockFileSystem)serializer.Deserialize(memoryStream);
memoryStream.Dispose();

// Assert
Assert.AreEqual(
expected,
fileSystem.GetFile(path).Contents);
Assert.AreEqual(
content,
fileSystem.File.ReadAllBytes(path));
}
}
}
1 change: 0 additions & 1 deletion System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class MockFileSystem : IFileSystem, IMockFileDataAccessor
private const string DEFAULT_CURRENT_DIRECTORY = @"C:\";

private readonly IDictionary<string, MockFileData> files;
[NonSerialized]
private readonly PathVerifier pathVerifier;

public MockFileSystem() : this(null) { }
Expand Down
1 change: 1 addition & 0 deletions System.IO.Abstractions.TestingHelpers/PathVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace System.IO.Abstractions.TestingHelpers
{
using XFS = MockUnixSupport;

[Serializable]
public class PathVerifier
{
private static readonly char[] AdditionalInvalidPathChars = { '*', '?' };
Expand Down