-
-
Notifications
You must be signed in to change notification settings - Fork 802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix multiple Mock.Setup() with Expression<Func<T>> invocation #584
Comments
@msharonov:
Your second code example does not make any sense to me. Could you perhaps show the definition of the |
Of course, my bad. Here is the whole public interface ITestInt
{
int Do(bool arg);
} I'm trying to archieve only one goal: take ability to define mocking method args with an expression, that received outside of initalization method. Little exampple [TestMethod]
public void TestMethod3()
{
var mock = new MockDefinition();
mock
.Setup(() => It.Is<bool>(_ => true), 1)
.Setup(() => It.Is<bool>(_ => false), -1);
//---
var trueResult = mock.Do(true);
var falseResult = mock.Do(false);
//---
Assert.AreEqual(trueResult, 1);
Assert.AreEqual(falseResult, -1); // fails here
}
public sealed class MockDefinition : ITestInt
{
private readonly Mock<ITestInt> _mock = new Mock<ITestInt>();
public MockDefinition Setup(Expression<Func<bool>> setupFuncExp, int result)
{
_mock.Setup(_ => _.Do(setupFuncExp.Compile().Invoke())).Returns(result);
return this;
}
public int Do(bool arg)
{
return _mock.Object.Do(arg);
}
} |
Why all the |
Because in real life |
Well in that case the problem can be reduced to passing the proper expression tree to Note that You will likely need to build the whole expression yourself, e.g. This appears to be a pure usage question (not a request for an enhancement, nor a bug report) and it's mostly about LINQ expression trees, as far as I can tell. Do you agree? |
Oh. Yes, you're right. Thanx!) |
I'm trying to use multiple Mock.Setup() invocation for the same mocked object method.
It works fine, when i'm using code like that:
But, when i'm trying to execute Mock.Setup() method with Expression<Func>, it fails. A little code example:
The text was updated successfully, but these errors were encountered: