Created
March 20, 2021 17:50
-
-
Save tjrobinson/4aac14e1bf71e72ac281549f3daed906 to your computer and use it in GitHub Desktop.
AutoMocker experiments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @nuget: Moq.AutoMock -Version 2.3.0 | |
using System; | |
using Moq; | |
using Moq.AutoMock; | |
public interface IMyService | |
{ | |
string GetValue(); | |
} | |
public class MyController | |
{ | |
private readonly IMyService myService; | |
public MyController(IMyService myService) | |
{ | |
this.myService = myService; | |
} | |
public string GetValueFromMyService() | |
{ | |
return this.myService.GetValue(); | |
} | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var mocker = new AutoMocker(); | |
var myServiceMock = new Mock<IMyService>(); | |
myServiceMock.Setup(m => m.GetValue()).Returns("Value from mock"); | |
mocker.Use(myServiceMock); // Or should it be: mocker.Use(myServiceMock.Object); | |
var myController = mocker.CreateInstance<MyController>(); | |
Console.WriteLine(myController.GetValueFromMyService()); | |
var myServiceMockFromMocker = mocker.GetMock<IMyService>(); | |
Console.WriteLine(myServiceMockFromMocker.GetType().FullName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment