繁体   English   中英

从模拟方法调用中调用的私有方法中的部分模拟测试失败

[英]Partial Mock Test fails on call in Private Method that was called from Mocked method call

下面,我有一个要测试的对象的代码。 我在测试中使用了部分模拟,并且在测试GenerateReport方法时想模拟对InsertData的调用。 由于某种原因,我的测试将继续通过InsertData到DoSomeStuff进行下去,并在Path.Combine行上失败。 我想念什么?

public class MyClass : IMyClass
{
    private readonly IMyClassDataProvider _myClassDataProvider;

    public virtual string WorkingDirectory { get; set; }

    public MyClass(IMyClassDataProvider myClassDataProvider)
    {
        _myClassDataProvider = myClassDataProvider;
    }

    internal virtual void ValidateWorkingDirectory()
    {
        if (string.IsNullOrEmpty(WorkingDirectory))
        {
            throw new ApplicationException("Must set Working Directory before any method calls.");
        }
    }

    public virtual void GenerateReport(vars)
    {
        ValidateWorkingDirectory();
        InsertData(_myClassDataProvider.GetData(vars), "ReportName"); 
                    //want to mock the call above
    }

    internal virtual void InsertData(DataSet analysis, string fileName)
    {
        DoSomeStuff();
    }

    private static void DoSomeStuff()
    {
        string fileName = FileNameProvider.Get();
        Path.Combine(WorkingDirectory, fileName); //fails here!
    }
}


[TestFixture]
public class MyClassTests : TestFixtureBase
{
    private MockRepository _mocks;
    private IMyClassDataProvider  _myClassDataProvider;
    private MyClass _myClass;
    private var _vars;

    [SetUp]
    protected void Init()
    {
        _mocks = new MockRepository();
        _myClassDataProvider = _mocks.StrictMock<IMyClassDataProvider >();
        _myClass = _mocks.PartialMock<MyClass>(_myClassDataProvider);
        _vars = "who cares";
    }

    [Test]
    public virtual void ShouldGenerateReport()
    {
        DataSet dataSetToReturn = new DataSet();
        using (_mocks.Ordered())
        {
            Expect.Call(() => _myClass.ValidateWorkingDirectory());
            Expect.Call(_myClassDataProvider.GetData(vars)).Return(dataSetToReturn);
            Expect.Call(() => _myClass.InsertData(dataSetToReturn, "ReportName"));
        }
        _mocks.ReplayAll();
        _myClass.GenerateReport(vars);
        _mocks.VerifyAll();
    }
}

将InsertData从内部更改为公共将解决此问题。 我假设Rhino模拟需要公开一个方法,以便能够模拟出该方法调用。

关于在期望值之后添加IgnoreArguements,我不太确定如何通过,因为在我尝试运行此代码时它仍然调用orignal方法。

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM