Skip to content
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

Infinite while for ConditionalContext with InnerMock Property get access #292

Closed
sGeeK44 opened this issue Oct 6, 2016 · 3 comments
Closed

Comments

@sGeeK44
Copy link

sGeeK44 commented Oct 6, 2016

Hi,

I would like return several result for a method and result conditionned by inner mock state.
When mocked Method is called in tested class, Conditionnal generate a stackOverflow Exception

To Reproduce behaviour:
In TestFile:

var dbResultFound = new Mock<IDataReader>();
dbResultFound.Setup(_ => _.Read()).Returns(true);
dbResultNotFound = new Mock<IDataReader>();
dbResultNotFound.Setup(_ => _.Read()).Returns(false);

var command = new Mock<IDbCommand>();
command.When(() => command.Object.CommandText == "SELECT * FROM TABLE WHERE ID=1")
       .Setup(_ => _.ExecuteReader())
       .Returns(DbResultFound.Object);
command.When(() => command.Object.CommandText == "SELECT * FROM TABLE WHERE ID=2")
       .Setup(_ => _.ExecuteReader())
       .Returns(DbResultNotFound.Object);

In TestedFile:

command.CommandText = "SELECT * FROM TABLE WHERE ID=2";
command.ExecuteReader();
@kzu
Copy link
Contributor

kzu commented Oct 11, 2016

Interesting scenario. I'm pretty sure that was not the originally intended design though, so I'm not super surprised it doesn't work ;)

@stakx
Copy link
Contributor

stakx commented Jul 2, 2017

The same thing can be achieved as follows:

var dbResultFound    = Mock.Of<IDataReader>(_ => _.Read() == true);
var dbResultNotFound = Mock.Of<IDataReader>(_ => _.Read() == false);

var command = new Mock<IDbCommand>();
command.SetupProperty(cmd => cmd.CommandText);
command.Setup(cmd => cmd.ExecuteReader()).Returns(() =>
    {
        switch (command.Object.CommandText)
        {
            case "SELECT * FROM TABLE WHERE ID=1": return dbResultFound;
            case "SELECT * FROM TABLE WHERE ID=2": return dbResultNotFound;

        }
    });

I'm not sure if this issue needs to be fixed at all... but it would be nice not to have infinite loops in Moq.

@stakx
Copy link
Contributor

stakx commented Jul 11, 2017

This will be fixed in the next release of Moq (version >4.7.63).

@stakx stakx closed this as completed Jul 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants