I have a interface that contains the following method signature:
TResult GetValue<T, TResult>(object key, Expression<Func<T, TResult>> property) where T : class;
Using Moq, I'm able to mock a specific call of this method like this:
var repo = new Mock<IRepository>();
repo.Setup(r => r.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId)).Returns("SecretAgentId");
Then when I do this call
repo.Object.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId);
Tt returns "SecretAgentId"
as I expect, so everything looks fine.
My problem is that in our real production code we use NSubstitute, and not Moq. I tried using the same type of setup here:
var repo = Substitute.For<ICrmRepository>();
repo.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId).Returns("SecretAgentId");
However, when I do the following call here
repo.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId);
It returns "" instead of "SecretAgentId"
I tried replacing c => c.SecretAgentId
with Arg.Any<Expression<Func<Customer, string>>>()
just to see if it works then, and then it returns "SecretAgentId"
as expected. But I need to verify that it is called with the correct expression, and not just any expression.
So I need to know if it is possible to get this to work in NSubstitute, and if it is, how?