NMock:重新定义方法期望

发布于 2024-12-21 05:06:34 字数 473 浏览 2 评论 0原文

我是 NMock 和一般嘲笑的新手。 是否有可能重新定义期望? 事实上,我想用很多方法来模拟一个接口。 所以我决定对通用方法期望进行因式分解,这样就不必将它们写 1000 次。 我的问题如下: 我有一种加载文件的存根方法。 在大多数情况下,模拟不会对此方法执行任何操作。 所以我在 [SetUp] 中分解了期望,

Stub.On(myMock).Method("Load").Will(Return.Value(True));

但是,在测试用例中,我想测试使用模拟的对象是否对异常响应良好,所以我放入了我的测试方法:

Stub.On(myMock).Method("Load").Will(Throw.Exception(new FileNotFoundException()));

当我调试测试时,我看到 Load方法返回 True。 我可以理解这一点,但是是否可以重置方法的异常或重新定义它?

I'm new to NMock and mocking in general.
Is it possible to redefine an expectation ?
In fact, I whant to mock an interface with many methods.
So I decided to factorize common method expectations not to have to write them 1000 times.
My issue is the following :
I have a method to stub that loads a file.
In most cases, the mock will do nothing for this method.
So I factorized the expectation in [SetUp]

Stub.On(myMock).Method("Load").Will(Return.Value(True));

But, In a test case, I want to test that the object using the mock responds well to an exception, so I put in my test method :

Stub.On(myMock).Method("Load").Will(Throw.Exception(new FileNotFoundException()));

When I debug the test, I see the Load method is returning True.
I can anderstand this, but is it possible to reset exceptation for a method or redefine it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-12-28 05:06:34

我从来没有找到一个好的方法来做到这一点。我必须使用自定义操作来根据特定调用的需要设置值。像这样的事情:

[TestFixture]
public class Testing
{
    public interface IXyz
    {
        bool Load();
    }

    public class DelegateAction<T> : NMock2.IAction
    {
        private Func<T> _resultFunc;

        public void SetResultFunction(Func<T> func)
        {
            _resultFunc = func;
        }

        public DelegateAction(Func<T> resultFunc)
        {
            _resultFunc = resultFunc;
        }


        public void Invoke(Invocation invocation)
        {
            invocation.Result = _resultFunc();
        }

        public void DescribeTo(TextWriter writer)
        {
        }
    }

    private bool _result = true;
    private DelegateAction<bool> _action;

    [Test]
    public void ResetTheReturnValue()
    {
        //would be done in general setup...
        Mockery mocker = new Mockery();
        IXyz test = mocker.NewMock<IXyz>();
        _action = new DelegateAction<bool>(() => _result);
        Stub.On(test).Method("Load").Will(_action);

        //Reset for test.... - if you comment it out true is 
                       //returned as default and the test passes
        _action.SetResultFunction(() => { throw new Exception();});

        Assert.IsTrue(test.Load());
    }
}

我通常不会允许设置该函数,因为我通常只想偶尔返回不同的值,这可以通过更改字段来完成。确保在测试结束时重置。

是的,我知道这很糟糕,如果有人知道的话,我会喜欢更好的方法。顺便说一句 - 如果你不喜欢 NMock,你可能想看看像 Moq 这样的东西。我倾向于用它得到更好的结果,尽管显然你的里程可能会有所不同:)

I've never found a nice way to do this. I've had to use a custom action to set the value as needed for the specific call. Something like this:

[TestFixture]
public class Testing
{
    public interface IXyz
    {
        bool Load();
    }

    public class DelegateAction<T> : NMock2.IAction
    {
        private Func<T> _resultFunc;

        public void SetResultFunction(Func<T> func)
        {
            _resultFunc = func;
        }

        public DelegateAction(Func<T> resultFunc)
        {
            _resultFunc = resultFunc;
        }


        public void Invoke(Invocation invocation)
        {
            invocation.Result = _resultFunc();
        }

        public void DescribeTo(TextWriter writer)
        {
        }
    }

    private bool _result = true;
    private DelegateAction<bool> _action;

    [Test]
    public void ResetTheReturnValue()
    {
        //would be done in general setup...
        Mockery mocker = new Mockery();
        IXyz test = mocker.NewMock<IXyz>();
        _action = new DelegateAction<bool>(() => _result);
        Stub.On(test).Method("Load").Will(_action);

        //Reset for test.... - if you comment it out true is 
                       //returned as default and the test passes
        _action.SetResultFunction(() => { throw new Exception();});

        Assert.IsTrue(test.Load());
    }
}

I wouldn't normally allow the function to be set as I'd generally just want to return a different value ocasionally, which could be done by changing the field. Make sure to reset things at the end of the test.

Yes, I know this is pretty crappy and would love a better way if anyone knows of one. As an aside - if you aren't stuck with NMock you might want to take a look at something like Moq instead. I tend to have better results with it, although obviously your mileage may vary :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文