Small Visual Studio 2022 extension, helping to write Moq tests for C#.
Download Moq.QuickMock.vsix
from latest successful build.
DemoClassOnly
class to mock:
public DemoClassOnly(ILogger<DemoClassOnly> logger,
string stringValue,
int intValue,
int? nullIntValue,
ICurrentUser currentUser,
Func<SomeCommand> cmdFactory,
Func<IValidator<InvoiceDetailsInput>> validatorFactory) { }
All these examples live in MyTestDemoClassOnlyTests.cs.
Put the cursor (caret)
between the ()
, and hit CTRL + .
.
var systemUnderTest = new DemoClassOnly(<cursor>);
Find Mock ctor (Moq)
Refactor Menu Options.
Refactor output:
var loggerMock = new Mock<ILogger<DemoClassOnly>>();
var currentUserMock = new Mock<ICurrentUser>();
var cmdFactoryMock = new Mock<Func<SomeCommand>>();
var validatorFactoryMock = new Mock<Func<IValidator<InvoiceDetailsInput>>>();
var systemUnderTest = new DemoClassOnly(loggerMock.Object,
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int?>(),
currentUserMock.Object,
cmdFactoryMock.Object,
validatorFactoryMock.Object);
Put the cursor (caret)
between the ()
, and hit CTRL + .
.
var systemUnderTest = new DemoClassOnly(<cursor>);
Find Quick mock ctor (Moq)
Refactor Menu Options.
Refactor output:
var systemUnderTest = new DemoClassOnly(Mock.Of<ILogger<DemoClassOnly>>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int?>(),
Mock.Of<ICurrentUser>(),
Mock.Of<Func<SomeCommand>>(),
Mock.Of<Func<IValidator<InvoiceDetailsInput>>>());
Put the cursor (caret)
on an argument where Mock.Of<T>
is used.
Find Mock.Of<T> to new Mock<T> (Moq)
Refactor Menu Options.
Make sure you put the cursor
on the word Mock
or just in front of it.
var systemUnderTest = new DemoClassOnly(Mock.Of<ILogger<DemoClassOnly>>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int?>(),
<cursor>Mock.Of<ICurrentUser>(),
Mock.Of<Func<SomeCommand>>(),
Mock.Of<Func<IValidator<InvoiceDetailsInput>>>());
Refactor output:
var currentUserMock = new Mock<ICurrentUser>();
var systemUnderTest = new DemoClassOnly(Mock.Of<ILogger<DemoClassOnly>>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int?>(),
currentUserMock.Object,
Mock.Of<Func<SomeCommand>>(),
Mock.Of<Func<IValidator<InvoiceDetailsInput>>>());
Put the cursor (caret)
on an argument where mock.Object
is used.
Find mock.Object to new Mock.Of<T> (Moq)
Refactor Menu Options.
Will only remove the variable if it is a local variable, and instantiated the a Mock
object on th e sane line.
// this will not be removed
Mock<Func<SomeCommand>> _cmdFactoryMock = new Mock<Func<SomeCommand>>();
[TestMethod()]
public void DemoClassOnlyTest_MockObject_to_MockOfT_local_variable()
{
// this will not be removed
Mock<ICurrentUser> currentUserMock;
currentUserMock = new Mock<ICurrentUser>();
// this variable will be removed
var validatorFactoryMock = new Mock<Func<IValidator<InvoiceDetailsInput>>>();
var systemUnderTest = new DemoClassOnly(Mock.Of<ILogger<DemoClassOnly>>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int?>(),
currentUserMock.Object,
_cmdFactoryMock.Object,
validatorFactoryMock.Object);
}
Refactor output:
// this will not be removed
Mock<Func<SomeCommand>> _cmdFactoryMock = new Mock<Func<SomeCommand>>();
[TestMethod()]
public void DemoClassOnlyTest_MockObject_to_MockOfT_local_variable()
{
// this will not be removed
Mock<ICurrentUser> currentUserMock;
currentUserMock = new Mock<ICurrentUser>();
var systemUnderTest = new DemoClassOnly(Mock.Of<ILogger<DemoClassOnly>>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int?>(),
Mock.Of<ICurrentUser>(),
Mock.Of<Func<SomeCommand>>(),
Mock.Of<Func<IValidator<InvoiceDetailsInput>>>());
}
NOTE: This extension will only change code following the file naming convention *tests.cs
eg: TheseAreMyHeroTests.cs
.
- Currently only supports C#, will need to give the VB.Net folks more love.
- pipeline to auto deploy to Visual Studio Marketplace
- Get it working for VB.Net .
- Unit testing extensions (or something like that).