我有一个包含以下方法签名接口:嘲弄了表达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中,如果是这样,怎么样?