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

Multiple return values of type task #144

Closed
xp-development opened this issue Nov 20, 2014 · 2 comments
Closed

Multiple return values of type task #144

xp-development opened this issue Nov 20, 2014 · 2 comments

Comments

@xp-development
Copy link

It is not possible to return different return values of type task. I receive always the same task. If the method returns "string" instead of "Task<string>", it works well!

The following test is a infinite loop:

public interface ITest
{
  Task<string> Write();
}

public class Test
{
  private readonly ITest test;

  public Test(ITest test)
  {
    this.test = test;
  }

  public void Write()
  {
    var result = test.Write().Result;
    while (result != "")
    {
      Console.WriteLine(result);
      result = test.Write().Result;
    }
  }
}

[TestMethod]
public void TaskTest()
{
  var queue = new Queue<string>();
  queue.Enqueue("qq");
  queue.Enqueue("");

  var mock = new Mock<ITest>();
  mock.Setup(item => item.Write()).Returns(Task.Run(() => queue.Dequeue()));

  var test = new Test(mock.Object);

  test.Write();
}
@mikeburdick
Copy link

Your setup needs deferred execution I think:

mock.Setup(item => item.Write()).Returns(() => { return Task.Run(() => queue.Dequeue(); }));

@xp-development
Copy link
Author

Great! It works!

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

2 participants