我用犀牛嘲笑3.6版,而我發現here在我的情況下不能正常工作了答案:Rhino Mocks:如何在沒有明確所有期望的情況下改變方法,財產或領域的期望?
[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();
}
而且我有一個InvalidOperationException:爲IConnected.Connect()的結果;已經建立。
我測試它與存根和模擬,我有相同的結果。
我做了與屬性相同的測試,它也不起作用。
[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();
}
這是Rhino Mocks的壞版本還是有迴歸?
,工作是清除所有的期望,但我必須重新設置爲相同值的斷言預期的唯一方法:
// clear expectations, an enum defines which
_stubRepository.BackToRecord(BackToRecordOptions.All);
// go to replay again.
_stubRepository.Replay();
我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;
}
你需要改變期望? –
我需要在調用Connect mehtod時更改IsConnected狀態。但在我的測試中,IsConnected值應該改變。它用於測試我的重新連接管理器的實現。在這篇文章中(http://stackoverflow.com/questions/770013/rhino-mocks-how-to-clear-previous-expectations-on-an-object),據說Repeat.Any覆蓋了所有先例的期望,但它並沒有「工作。 – Brice2Paris