yuvalm2
yuvalm2

Reputation: 992

Generating lambda expressions in nsubstitute mocking

I am trying to get an NSubstitue object I have to mock a function by using another. (For example, I want an identity function getting a single string, and returning it)

Suppose I had a class foo with function bar getting a single int, and returning a single int, how would I create a mock of it which would return the value it got for every input?

In particular, given an interface:

public interface ifoo 
{
   public int func(int x) { ... }
}

Could I create a mock of it using NSubstitute, and make the function func returh for every x, that x? (i.e. f(x) = x)

Upvotes: 1

Views: 1121

Answers (1)

Vidmantas Blazevicius
Vidmantas Blazevicius

Reputation: 4812

Can use CallInfo to return the argument in the first position

var subst = Substitute.For<ifoo>();
subst.func(Arg.Any<int>()).Returns(info => info.ArgAt<int>(0));
var test = subst.func(10);

Upvotes: 2

Related Questions