Skip to content

Instantly share code, notes, and snippets.

@tjrobinson
Created March 20, 2021 17:50
Show Gist options
  • Save tjrobinson/4aac14e1bf71e72ac281549f3daed906 to your computer and use it in GitHub Desktop.
Save tjrobinson/4aac14e1bf71e72ac281549f3daed906 to your computer and use it in GitHub Desktop.
AutoMocker experiments
// @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