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

Is it possible to mock local variable in a method #302

Closed
garygao opened this issue Nov 28, 2016 · 1 comment
Closed

Is it possible to mock local variable in a method #302

garygao opened this issue Nov 28, 2016 · 1 comment

Comments

@garygao
Copy link

garygao commented Nov 28, 2016

For example I have a method like this:

public class TaskService
{
    public List<Task> GetTasks(int contactId, DataContext dbContext)
    {
         ITaskRepository taskRepo = new TaskRepository(dbContext);
         List<Task> tasks = taskRepo.GetTasksFromDB(contactId).ToList();
         ......
         return tasks;
    }
}

How can I mock the local variable taskRepo when I writing test for method GetTasks?
I have test code like this:

public void GetTasksTest()
{
      var taskService = new Mock<TaskService>();
      var tasks = new List<Task> { new Task { Id = 111 } };
      // But how to mock the method `taskRepo.GetTasksFromDB(contactId).ToList()`?
      // And I don't want to pass taskRepo as a parameter.
}
@kzu
Copy link
Contributor

kzu commented Nov 28, 2016

If you can't write a manual mock that does it in plain C#, you can't with Moq.

This is one such case.

@kzu kzu closed this as completed Nov 28, 2016
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