Parallel.ForEach 中的模拟方法总是 returns null

Mock method in Parallel.ForEach always returns null

我有以下代码:

public int LoadFilesAndSaveInDatabase(string filesPath)
{
    var calls = new ConcurrentStack<GdsCallDto>();

    var filesInDirectory = this._directoryProxy.GetFiles(filesPath);
    if (filesInDirectory.Any())
    {
        Parallel.ForEach(filesInDirectory, file =>
        {
            var lines = this._fileProxy.ReadAllLines(file, Encoding.Unicode);
            if (lines.Any())
            {
                // Reads the file and setup a new DTO.
                var deserializedCall = this._fileManager.DeserializeFileContent(lines, Path.GetFileName(file));

                // Insert the DTO in the database.
                this._gdsCallsData.InsertOrUpdateGdsCall(deserializedCall);

                // We keep track of the dto to count the number of restored items.
                calls.Push(deserializedCall);
            }
        });
    }
    return calls.Count;
}

我有以下单元测试:

[TestMethod]
public void ShouldLoadFilesAndSaveInDatabase()
{
    // Arrange
    var path = RandomGenerator.GetRandomString(56);
    var encoding = Encoding.Unicode;
    var fileNameEnvironment = RandomGenerator.GetRandomString();
    var fileNameModule = RandomGenerator.GetRandomString();
    var fileNameRecordLocator = RandomGenerator.GetRandomString(6);
    var fileNameTimestamp = RandomGenerator.GetRandomDateTime().ToString("O").Replace(':', 'o');

    // We simulate the presence of 4 files.
    var files = new List<string>
    {
        RandomGenerator.GetRandomString(255),
        RandomGenerator.GetRandomString(255),
        RandomGenerator.GetRandomString(255),
        RandomGenerator.GetRandomString(255)
    }.ToArray();

    var expectedResult = 4;

    this._directoryProxy.Expect(d => d.GetFiles(path))
        .Return(files);

    this._fileProxy.Expect(f => f.ReadAllLines(path, encoding))
        .Return(files).Repeat.Times(files.Length);

    // Act
    var result = this._databaseReloadManager.LoadFilesAndSaveInDatabase(path);

    // Assert
    Assert.AreEqual(result, expectedResult);
    this._directoryProxy.AssertWasCalled(d => d.GetFiles(path));
    this._fileProxy.AssertWasCalled(f => f.ReadAllLines(path, Encoding.Unicode));
}

问题出在以下行:

var lines = this._fileProxy.ReadAllLines(file, Encoding.Unicode);

即使我设置了期望值和 return 值,当我 运行 单元测试时,它总是 return 为空。

我正在使用 Rhino.Mocks,它在其他地方工作得很好,但在那里不行。

我在这里查看了一些讨论,但其中 none 对我有所帮助。会不会是因为使用了Parallel.ForEach?有没有办法做这样的模拟?

如果您需要任何其他信息,请告诉我。

我认为并行化没有问题。看来您的问题与使用 Rhino Mock 设置代理实例有关。

确保您传入 ReadAllLines 参数的内容与您通过生产代码 运行 调用它们的内容相同。

this._fileProxy.Expect(f => f.ReadAllLines(path, encoding))
        .Return(files).Repeat.Times(files.Length);

例如,如果您在不同的 路径 上设置并且在执行测试时该路径的值不同,您可能会在 return 中看到 NULL。但是如果不在代码中看到完整的 setup/constructor 就很难分辨。还要检查随机生成器,看看每次都使用了什么。

以下是我整理并为我工作的。 工作意味着我没有得到 NULL:

var lines = this._fileProxy.ReadAllLines(file, Encoding.Unicode);

//some dummy code so I can compile
public interface IProxyDir
{
    IEnumerable<string> GetFiles(string path);
}

public class GdsCallDto
{
}

public class Proxy : IProxyDir
{
    public IEnumerable<string> GetFiles(string path)
    {
        throw new NotImplementedException();
    }
}

public interface IFileDir
{
    IEnumerable<string> ReadAllLines(string path, Encoding encoding);
}

public class FileProxy : IFileDir
{
    public IEnumerable<string> ReadAllLines(string path, Encoding encoding)
    {
        throw new NotImplementedException();
    }
}

public interface IFileMgr
{
    string DeserializeFileContent(IEnumerable<string> lines, string content);
}

public class FileMgr : IFileMgr
{
    public string DeserializeFileContent(IEnumerable<string> lines, string content)
    {
        throw new NotImplementedException();
    }
}

//system under test
public class Sut
{
    private IProxyDir _directoryProxy;
    private IFileDir _fileProxy;
    private IFileMgr _fileManager;

    public Sut(IProxyDir proxyDir,  IFileDir fileProxy, IFileMgr mgr)
    {
        _fileManager = mgr;
        _directoryProxy = proxyDir;
        _fileProxy = fileProxy;
    }

    public int LoadFilesAndSaveInDatabase(string filesPath)
    {
        var calls = new ConcurrentStack<GdsCallDto>();

        var filesInDirectory = this._directoryProxy.GetFiles(filesPath);
        if (filesInDirectory.Any())
        {
            Parallel.ForEach(filesInDirectory, file =>
            {
                var lines = this._fileProxy.ReadAllLines("ssss", Encoding.Unicode);

                if (lines.Any())
                {
                    // Reads the file and setup a new DTO.
                    var deserializedCall = this._fileManager.DeserializeFileContent(lines, Path.GetFileName("file"));

                    // Insert the DTO in the database.
                    //this._gdsCallsData.InsertOrUpdateGdsCall(deserializedCall);

                    // We keep track of the dto to count the number of restored items.
                    //calls.Push(deserializedCall);
                }
            });
        }

        return 1;
    }
}

样本单元测试

[TestClass]
public class UnitTest1
{
    private IProxyDir _directoryProxy;
    private IFileDir _fileProxy;
    private IFileMgr _fileMgr;

    private Sut _sut;

    public UnitTest1()
    {
        _directoryProxy = MockRepository.GenerateMock<IProxyDir>();
        _fileProxy = MockRepository.GenerateMock<IFileDir>();
        _fileMgr = MockRepository.GenerateMock<IFileMgr>();
    }

    [TestMethod]
    public void ShouldLoadFilesAndSaveInDatabase()
    {
        // Arrange
        var path = RandomGenerator.GetRandomString(56);
        var encoding = Encoding.Unicode;
        var fileNameEnvironment = RandomGenerator.GetRandomString(5);
        var fileNameModule = RandomGenerator.GetRandomString(5);
        var fileNameRecordLocator = RandomGenerator.GetRandomString(6);
        var fileNameTimestamp = RandomGenerator.GetRandomDateTime().ToString("O").Replace(':', 'o');

        // We simulate the presence of 4 files.
        var files = new List<string>
        {
            RandomGenerator.GetRandomString(255),
            RandomGenerator.GetRandomString(255),
            RandomGenerator.GetRandomString(255),
            RandomGenerator.GetRandomString(255)
        }.ToArray();

        var expectedResult = 4;

        this._directoryProxy.Expect(d => d.GetFiles(path))
            .Return(files);

        this._fileProxy.Expect(f => f.ReadAllLines(path, encoding))
    .Return(files).Repeat.Times(files.Length);

        _sut = new Sut(_directoryProxy, _fileProxy, _fileMgr);

        // Act
        var result = this._sut.LoadFilesAndSaveInDatabase(path);

        // Assert
        Assert.AreEqual(result, expectedResult);
        this._directoryProxy.AssertWasCalled(d => d.GetFiles(path));
        this._fileProxy.AssertWasCalled(f => f.ReadAllLines(path, Encoding.Unicode));
    }

}

internal class RandomGenerator
{
    public static string GetRandomString(int number)
    {
        return "ssss";
    }

    public static DateTime GetRandomDateTime()
    {
        return new DateTime();
    }
}

我可以解决这个问题,可能是使用随机值造成的。我现在按照我的期望调用方法 IgnoreArguments():

this._fileProxy.Expect(f => f.ReadAllLines(path, encoding))
    .Return(files).Repeat.Times(files.Length).IgnoreArguments();

它成功了(即单元测试 运行 成功),但我不知道它是否非常优雅。