2013-04-09 88 views
2

有沒有辦法模擬一個需要動態參數的方法?帶動態參數的RhinoMocks期望

我想設置的期望是這樣的:

_hasher.Expect(h => h.ComputeHash(Arg<dynamic>.Matches(o=> o.PropertyA == "123"))).Return("some hash"); 

我得到錯誤:表達式樹不能包含動態表情。我當然可以做這樣的事情:

_hasher.Expect(h => h.ComputeHash(Arg<object>.Is.Anything)).Return("some hash"); 

但我覺得這是在我的測試中留下一個空白。是否有任何其他替代方法來模擬依賴,它有一個接受動態參數的方法?

回答

2

試試這個:

_hasher.Expect(h => h.ComputeHash(Arg<object>.Is.Anything)).Return("some hash") 
    .WhenCalled(x => 
     { 
      dynamic actual = x.Arguments[0]; 
      Assert.AreEqual("123", actual.PropertyA); 
     }); 

這是一個黑客攻擊的一位,當然,但它的工作原理,以及它給你有用的信息時,測試失敗。