1
votes

I use Rhino Mocks version 3.6 and the answer that I found here doesn't work in my case :

    [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(); 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 ?

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 :

/// <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;
}
1
What do you need to change an expectation for?Alexander Stepaniuk
I need to change the IsConnected status when Connect mehtod is called. But during my test the IsConnected value should change. It's for testing the implementation of my reconnection manager. In this post (stackoverflow.com/questions/770013/…) it's said that Repeat.Any override all precedent expectation but it doesn't work.Brice2Paris

1 Answers

0
votes

You can implement some behavior for stubs via Do() handler.
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.