首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rhino模拟AAA ExpectationViolationException

Rhino模拟AAA ExpectationViolationException
EN

Stack Overflow用户
提问于 2012-02-29 14:36:17
回答 1查看 606关注 0票数 2

运行rhinomock测试方法时出错:测试方法TestProject1.UnitTest2.TestMethod1引发异常: Rhino.Mocks.Exceptions.ExpectationViolationException: ITestInterface.Method1(5);应为#1,实际为#0。

我的代码如下所示:

代码语言:javascript
复制
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;
        }
    }
}

我的测试看起来像这样:

代码语言:javascript
复制
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));

        }
    }
}

感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-29 14:46:33

您希望调用ITestInterface.Method1,但它从来没有实现过。

在测试代码中根本不使用mockProxy -您只需创建它,然后创建自己的实例,但两者之间没有关系。

您的TestClass不依赖于您想要模拟的任何接口,使用模拟的类似示例如下:

代码语言:javascript
复制
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?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9494609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档