简体   繁体   English

Rhino Mocks:如何在不明确所有期望的情况下更改方法,属性或字段的期望?

[英]Rhino Mocks : How to change expectation on method, property or field without clear all expectation?

I use Rhino Mocks version 3.6 and the answer that I found here doesn't work in my case : 我使用的是Rhino Mocks 3.6版,在这种情况下,我在这里找不到答案:

    [TestMethod()]
    public void Test()
    {
        IConnected connectable = MockRepository.GenerateStub<IConnected>();
        connectable.Stub(c => c.Connect()).Do(new Action(() =>
        {
            bool test = false;
            if (!test)
                test = true;
        })).Repeat.Any();
        connectable.Stub(c => c.Connect()).Do(new Action(() => { })).Repeat.Any();
    }

And I've got an InvalidOperationException: The result for IConnected.Connect(); 我有一个InvalidOperationException:IConnected.Connect();的结果; has already been setup. 已经设置好了。

I tests it with stub and mock and I've got same results. 我用存根和模拟测试了它,并且得到了相同的结果。

I made the same test with property and it doesn't work too. 我对属性进行了相同的测试,但它也不起作用。

    [TestMethod()]
    public void Test()
    {
        IConnected connectable = MockRepository.GenerateStub<IConnected>();
        connectable.Stub(c => c.IsConnected).Return(true).Repeat.Any();
        connectable.Stub(c => c.IsConnected).Return(false).Repeat.Any();
    }

Is it a bad version of Rhino Mocks or is there a regression ? 是Rhino Mocks的较差版本,还是有回归?

The only method that work is to clear all expectation but I must reset to same value all aver expectations : 唯一可行的方法是清除所有期望,但我必须将所有平均期望重置为相同的值:

// clear expectations, an enum defines which
_stubRepository.BackToRecord(BackToRecordOptions.All);
// go to replay again.
_stubRepository.Replay();

My IConnected interface : 我的IConnected接口:

/// <summary>
/// Represents connected component management interface.
/// </summary>
public interface IConnected
{
    /// <summary>
    /// Gets the connection status.
    /// </summary>
    ConnectionState ConnectionStatus { get; }
    /// <summary>
    /// Gets a value indicating whether this instance is connected.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance is connected; otherwise, <c>false</c>.
    /// </value>
    bool IsConnected { get; }
    /// <summary>
    /// Occurs when [connection state changed].
    /// </summary>
    event EventHandler<ConnectionStateChangeEventArgs> ConnectionStateChanged;
    /// <summary>
    /// Connects this instance.
    /// </summary>
    void Connect();
    /// <summary>
    /// Disconnects this instance.
    /// </summary>
    void Disconnect();
    /// <summary>
    /// Occurs when [reconnect].
    /// </summary>
    event EventHandler<ConnectionRetryEventArgs> RetryConnection;
}

You can implement some behavior for stubs via Do() handler. 您可以通过Do()处理程序为存根实现一些行为。
Here is a solution for your case: 这是您的情况的解决方案:

var isConnected = false;

var stub = MockRepository.GenerateStub<IConnected>();

stub
    .Stub(c => c.IsConnected)
    .Do((Func<bool>)(() => isConnected))
    .Return(false);

stub
    .Stub(c => c.Connect())
    .Do((Action)(() => { isConnected = true; }));

now just test: 现在只需测试:

Console.WriteLine(stub.IsConnected);
stub.Connect();
Console.WriteLine(stub.IsConnected);

But it would be much better if you redesign your tests to avoid cases when you need such a complex stub (of cource if it is possible). 但是,如果您重新设计测试以免需要复杂的存根(如果可能的话),那会更好。 Probably split test into a few more tests might be suitable. 可能将测试拆分为更多的测试可能是合适的。

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

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