我有一個包含以下方法簽名接口:嘲弄了表達NSubstitute
TResult GetValue<T, TResult>(object key, Expression<Func<T, TResult>> property) where T : class;
使用起訂量,我可以嘲笑這種方法的這樣一個特定的呼叫:
var repo = new Mock<IRepository>();
repo.Setup(r => r.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId)).Returns("SecretAgentId");
然後,當我做到這一點的呼叫如我所料
repo.Object.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId);
Tt的返回"SecretAgentId"
,所以一切都看起來很好。
我的問題是,在我們的真實生產代碼中,我們使用NSubstitute而不是Moq。我嘗試使用相同類型的安裝位置:
var repo = Substitute.For<ICrmRepository>();
repo.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId).Returns("SecretAgentId");
然而,當我做以下調用這裏
repo.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId);
它返回「」,而不是"SecretAgentId"
我試着用Arg.Any<Expression<Func<Customer, string>>>()
更換c => c.SecretAgentId
只是爲了看看它是否有效,然後按預期返回"SecretAgentId"
。但我需要驗證它是用正確的表達式調用的,而不是任何表達式。
所以我需要知道是否有可能讓這個工作在NSubstitute中,如果是這樣,怎麼樣?