LightMock.Generator 1.2.2

dotnet add package LightMock.Generator --version 1.2.2                
NuGet\Install-Package LightMock.Generator -Version 1.2.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LightMock.Generator" Version="1.2.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LightMock.Generator --version 1.2.2                
#r "nuget: LightMock.Generator, 1.2.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LightMock.Generator as a Cake Addin
#addin nuget:?package=LightMock.Generator&version=1.2.2

// Install LightMock.Generator as a Cake Tool
#tool nuget:?package=LightMock.Generator&version=1.2.2                

LightMock.Generator

Source generator that generates mocks by provided interfaces, classes with virtual and/or abstract members and delegates. Available on nuget. You should be familiar with LightMock because this project uses it underhood.

How to use

Use Mock<T> where T is your class, interface or delegate to batch create MockContext<T> and mock object.

Example with interface

using System;
using LightMock;
using LightMock.Generator;
using Xunit;

namespace Playground
{
    public interface IFoo
    {
        void Foo(int baz);
        int Bar();
        string Baz { get; set; }
        ref string Quux();
    }

    public class SomeTests
    {
        [Fact]
        public void Test()
        {
            var mock = new Mock<IFoo>();
            var o = mock.Object; // use Mock<T>.Object property to get mock object

            o.Foo(123);
            mock.Assert(f => f.Foo(123)); // Mock<T> uses MockContext<T> internally. Use it to assert or arrange context.

            o.Baz = "456"; 
            mock.AssertSet(f => f.Baz = The<string>.Is(s => s == "456")); // There methods available to work with properties.
            // See IMock<T>, IAdvancedMockContext<T> and IMockContext<T> to completed list

            const int expectedBar = 123;
            mock.Arrange(f => f.Bar()).Returns(expectedBar); // Mock<T> uses MockContext<T> internally. Use it to assert or arrange context.
            Assert.Equal(expectedBar, o.Bar());

            int bazInvokedTimes = 0; // ArrangeSetter without suffix uses AOT transformation. Methods with suffix can be used
            mock.ArrangeSetter_WhenAny(f => f.Baz = "").Callback<string>(s => bazInvokedTimes++); //  without AOT transformations.
            o.Baz = "some random value";
            Assert.Equal(1, bazInvokedTimes);

            const string EXPECTED_STRING = nameof(EXPECTED_STRING);
            // You can arrange and assert "ref return" methods using a RefReturn() extension method
            mock.RefReturn().Arrange(f => f.Quux()).Returns(() => EXPECTED_STRING);
            mock.RefReturn().Assert(f => f.Quux(), Invoked.Never);
            Assert.Equal(EXPECTED_STRING, o.Quux());
            // The RefReturn() extension method is generated for interfaces and classes
            mock.RefReturn().Assert(f => f.Quux(), Invoked.Once);
        }
    }
}

Example with class

using System;
using LightMock.Generator;
using Xunit;

namespace Playground
{
    public abstract class AFoo
    {
        public AFoo(int p1, int p2)
        { }

        public abstract void Foo(int p);
        public abstract int Bar();

        protected abstract void Baz(int p);
        protected abstract int Quux();

        public void InvokeBaz(int p) => Baz(p);
        public int InvokeQuux() => Quux();
    }

    public class SomeTests
    {
        [Fact]
        public void Test()
        {
            const int expected = 123;
            // To invoke a constructor of class place parameters in Mock<T> constructor
            var mock = new Mock<AFoo>(12, 45);
            // To arrange or assert protected members call Protected() extension function.
            // It and corresponding interface will be generated only for classes
            mock.Protected().Arrange(f => f.Quux()).Returns(expected);

            Assert.Equal(expected, mock.Object.InvokeQuux());
            mock.Protected().Assert(f => f.Quux());

            // To arrange or assert public members use Mock<T> functions
            mock.Arrange(f => f.Bar()).Returns(expected);
            Assert.Equal(expected, mock.Object.Bar());
            mock.Assert(f => f.Bar());
        }
    }
}

Example with delegate

using LightMock;
using LightMock.Generator;
using System;
using Xunit;

namespace Playground
{
    public class SomeTests
    {
        [Fact]
        public void TestDelegate()
        {
            var expectedObject = new object();
            var expectedArgs = new EventArgs();
            var mock = new Mock<EventHandler>();

            // don't use f => f(args), because LightMock doesn't support that.
            mock.Assert(f => f.Invoke(The<object>.IsAnyValue, The<EventArgs>.IsAnyValue), Invoked.Never);
            mock.Object(expectedObject, expectedArgs);
            mock.Assert(f => f.Invoke(The<object>.IsAnyValue, The<EventArgs>.IsAnyValue));
            mock.Assert(f => f.Invoke(expectedObject, expectedArgs));
        }
    }
}

Additional information

DisableCodeGenerationAttribute

Place the attribute to your assembly to disable the source code generator. It can be useful if you moving mocks to separate assembly. Be aware you can't use methods ArrangeSetter and AssertSet of Mock<T>, because they use AOT transformations.

DontOverrideAttribute

Use the attribute with class type whose virtual members should not be overridden

LightMockGenerator_Enable

Use the compiler property in your csproj file with "false" value to disable the source code generator. It can be useful if you moving mocks to separate assembly. Be aware: the compiler property will work if you install a nuget package of the generator into your project.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on LightMock.Generator:

Package Downloads
LightMoq

Extensions for LightMock.Generator to make it more like Moq.

RBG_GodotTools_AutoTest

RBG_GodotTools_AutoTest description.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on LightMock.Generator:

Repository Stars
chickensoft-games/GodotEnv
Manage Godot versions and addons from the command line on Windows, macOS, and Linux.
Version Downloads Last updated
1.2.2 12,158 1/26/2023
1.2.1 302 1/25/2023
1.2.0 300 12/8/2022
1.2.0-preview.3 115 11/22/2022
1.2.0-preview.2 111 11/14/2022
1.2.0-preview.1 113 11/8/2022
1.1.2 913 8/4/2022
1.1.1 472 4/5/2022
1.1.0 434 3/24/2022
1.1.0-preview.3 162 12/3/2021
1.1.0-preview.2 174 11/4/2021
1.0.1 343 4/15/2021
1.0.0 329 4/12/2021
1.0.0-preview.3 149 3/30/2021
1.0.0-preview.2 197 3/11/2021
1.0.0-preview.1 175 2/17/2021
0.904.0-beta 247 2/10/2021
0.903.0-beta 235 2/9/2021
0.902.0-beta 265 2/5/2021
0.901.0-beta 239 2/3/2021
0.900.0-beta 223 2/1/2021
0.99.0-beta 221 1/26/2021
0.98.0-beta 212 1/25/2021
0.97.0-beta 247 1/22/2021
0.96.0-beta 249 1/20/2021
0.95.0-beta 210 1/20/2021
0.94.0-beta 215 1/19/2021
0.93.0-beta 223 12/29/2020
0.92.0-beta 321 12/18/2020
0.91.0-beta 299 12/17/2020
0.90.0-beta 312 12/17/2020

* feat: additional way to avoid aggressive linker that can remove class attributes. #61