运行rhinomock测试方法时出错:测试方法TestProject1.UnitTest2.TestMethod1引发异常: Rhino.Mocks.Exceptions.ExpectationViolationException: ITestInterface.Method1(5);应为#1,实际为#0。
我的代码如下所示:
namespace ClassLibrary1
{
public interface ITestInterface
{
bool Method1(int x);
int Method(int a);
}
internal class TestClass : ITestInterface
{
public bool Method1(int x)
{
return true;
}
public int Method(int a)
{
return a;
}
}
}我的测试看起来像这样:
using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
namespace TestProject1
{
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod1()
{
ITestInterface mockProxy = MockRepository.GenerateMock<ITestInterface>();
TestClass tc = new TestClass();
bool result = tc.Method1(5);
Assert.IsTrue(result);
mockProxy.AssertWasCalled(x => x.Method1(5));
}
}
}感谢您的帮助。
发布于 2012-02-29 14:46:33
您希望调用ITestInterface.Method1,但它从来没有实现过。
在测试代码中根本不使用mockProxy -您只需创建它,然后创建自己的实例,但两者之间没有关系。
您的TestClass不依赖于您想要模拟的任何接口,使用模拟的类似示例如下:
internal class TestClass
{
private ITestInterface testInterface;
public TestClass(ITestInterface testInterface)
{
this.testInterface = testInterface;
}
public bool Method1(int x)
{
testInterface.Method1(x);
return true;
}
}
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod1()
{
ITestInterface mockProxy = MockRepository.GenerateMock<ITestInterface>();
TestClass tc = new TestClass(mockProxy);
bool result = tc.Method1(5);
Assert.IsTrue(result);
mockProxy.AssertWasCalled(x => x.Method1(5));
}
}我认为你应该阅读更多关于使用Rhino Mock的知识,例如Rhino Mocks AAA Quick Start?
https://stackoverflow.com/questions/9494609
复制相似问题