Skip to content

Latest commit

 

History

History
211 lines (154 loc) · 8.04 KB

README.md

File metadata and controls

211 lines (154 loc) · 8.04 KB

GitHub Workflow Status for main branch GitHub Visual Studio Marketplace Installs

drawing Pay it forward drawing

Moq.QuickMock

Small Visual Studio 2022 extension, helping to write Moq tests for C#.


Visual Studio Marketplace

Moq.QuickMock 2022

Download Moq.QuickMock.vsix and install

Download Moq.QuickMock.vsix from latest successful build.


Available Refactors


Scenario

DemoClassOnly class to mock:

public DemoClassOnly(ILogger<DemoClassOnly> logger,
                     string stringValue,
                     int intValue,
                     int? nullIntValue,
                     ICurrentUser currentUser,
                     Func<SomeCommand> cmdFactory,
                     Func<IValidator<InvoiceDetailsInput>>  validatorFactory) { }

Refactors

All these examples live in MyTestDemoClassOnlyTests.cs.

Mock ctor (Moq)

Put the cursor (caret) between the (), and hit CTRL + ..

var systemUnderTest = new DemoClassOnly(<cursor>);

Find Mock ctor (Moq) Refactor Menu Options.

Mock Ctor Demo

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);

Quick mock ctor (Moq)

Put the cursor (caret) between the (), and hit CTRL + ..

var systemUnderTest = new DemoClassOnly(<cursor>);

Find Quick mock ctor (Moq) Refactor Menu Options.

Quick Mock Ctor Demo

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>>>());

Mock.Of<T> to new Mock<T> (Moq)

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>>>());

Mock of to new mock Demo

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>>>());

mock.Object to Mock.Of<T>

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);
}

Mock Object to Mock Of remove local variable Demo

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.


Know issues

  • Currently only supports C#, will need to give the VB.Net folks more love.

ToDos

Tasks (Priority ordered)

Investigations

  • Unit testing extensions (or something like that).