部分模拟测试在从模拟方法调用中调用的私有方法中调用失败
下面,我有一个我想测试的对象的代码。 我在测试中使用部分模拟,并希望在测试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();
}
}
Below, I have code for an object that I would like to test.
I am using a partial mock in my test and want to mock out the call to InsertData when I am testing the GenerateReport method.
For some reason, my test continues down through InsertData to DoSomeStuff and fails on the Path.Combine line. What am I missing?
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 InsertData 从内部更改为公共将解决此问题。我假设一个方法需要公开,Rhino 模拟才能模拟方法调用。
关于在期望之后添加 IgnoreArguements,我不太确定这是如何通过的,因为当我尝试运行此代码时它仍然调用原始方法。
希望这有帮助
Changing InsertData from internal to public will fix this. I'm assuming a method needs to be public for Rhino mocks to be able to mock out the method call.
Concerning adding IgnoreArguements after the expectation, I'm not too sure how this passed because it still called the orignal method when I tried running this code.
Hope this helps