RSCG – MockMe
RSCG – MockMe
name | MockMe |
nuget | https://www.nuget.org/packages/MockMe/ |
link | https://github.com/connorivy/MockMe/ |
author | connorivy |
Creating mocks for testing classes
This is how you can use MockMe .
The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | < Project Sdk = "Microsoft.NET.Sdk" > < PropertyGroup > < TargetFramework >net9.0</ TargetFramework > < ImplicitUsings >enable</ ImplicitUsings > < Nullable >enable</ Nullable > < IsPackable >false</ IsPackable > < IsTestProject >true</ IsTestProject > </ PropertyGroup > < ItemGroup > < PackageReference Include = "Microsoft.NET.Test.Sdk" Version = "17.5.0" /> < PackageReference Include = "MockMe" Version = "1.1.2" /> < PackageReference Include = "MSTest.TestAdapter" Version = "2.2.10" /> < PackageReference Include = "MSTest.TestFramework" Version = "2.2.10" /> < PackageReference Include = "coverlet.collector" Version = "3.2.0" /> </ ItemGroup > < ItemGroup > < ProjectReference Include = "..\MockData\MockData.csproj" /> </ ItemGroup > < PropertyGroup > < EmitCompilerGeneratedFiles >true</ EmitCompilerGeneratedFiles > < CompilerGeneratedFilesOutputPath >$(BaseIntermediateOutputPath)\GX</ CompilerGeneratedFilesOutputPath > </ PropertyGroup > </ Project > |
The code that you will use is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | using MockMe; namespace TestClock; [TestClass] public class TestClock { [TestMethod] public void TestMyClock() { var mock = Mock.Me( default (MyClock)); mock.Setup.GetUtcNow().Returns(DateTime.Now.AddYears(-1)); mock.Setup.GetNow().Returns(DateTime.Now.AddYears(-1)); MyClock clock = mock; Assert.AreEqual(DateTime.Now.AddYears(-1).Year, clock.GetNow().Year); } } |
The code that is generated is
1 |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | // <auto-generated /> #pragma warning disable using System; namespace MockMe { internal static partial class Mock { public static object Me(global::MockMe.DummyClass unusedInstance) { throw new global::System.NotImplementedException(); } } } #pragma warning restore |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | // <auto-generated /> #pragma warning disable #nullable enable namespace MockMe { internal static partial class Mock { [global::System.CodeDom.Compiler.GeneratedCode( "MockMe" , "1.1.2.0" )] public static global::MockMe.Generated.MockData.MyClockMock Me(global::MockData.MyClock? unusedInstance) { return new (); } } } #pragma warning restore |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | // <auto-generated /> #pragma warning disable #nullable enable using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Reflection; using HarmonyLib; using MockMe; using MockMe.Mocks; using MockMe.Mocks.ClassMemberMocks; using MockMe.Mocks.ClassMemberMocks.CallTracker; namespace MockMe.Generated.MockData { [global::System.CodeDom.Compiler.GeneratedCode( "MockMe" , "1.1.2.0" )] internal class MyClockMock : global::MockMe.Abstractions.SealedTypeMock<global::MockData.MyClock> { public MyClockMock() { this .Setup = new MyClockMockSetup(); this .CallTracker = new MyClockMockSetup.MyClockMockCallTracker( this .Setup); this .Assert = new MyClockMockSetup.MyClockMockCallTracker.MyClockMockAsserter( this .CallTracker); global::MockMe.MockStore<global::MockData.MyClock>.Store.TryAdd( this .MockedObject, this ); } public MyClockMockSetup Setup { get ; } public MyClockMockSetup.MyClockMockCallTracker.MyClockMockAsserter Assert { get ; } private MyClockMockSetup.MyClockMockCallTracker CallTracker { get ; } internal sealed class Patch23f715ba229342e08d6fb11494a17f90 { private static bool Prefix(global::MockData.MyClock __instance, ref global::System.DateTime __result) { if (global::MockMe.MockStore<global::MockData.MyClock>.TryGetValue<MyClockMock>(__instance, out var mock)) { __result = mock.CallTracker.GetNow(); return false ; } return true ; } } internal sealed class Patch147ac8e5593f4d678d1855b2e81726e7 { private static bool Prefix(global::MockData.MyClock __instance, ref global::System.DateTime __result) { if (global::MockMe.MockStore<global::MockData.MyClock>.TryGetValue<MyClockMock>(__instance, out var mock)) { __result = mock.CallTracker.GetUtcNow(); return false ; } return true ; } } static MyClockMock() { var harmony = new global::HarmonyLib.Harmony( "com.mockme.patch" ); var originalPatch23f715ba229342e08d6fb11494a17f90 = typeof (global::MockData.MyClock).GetMethod( "GetNow" , new Type[] { } ); var Patch23f715ba229342e08d6fb11494a17f90 = typeof (Patch23f715ba229342e08d6fb11494a17f90).GetMethod( "Prefix" , global::System.Reflection.BindingFlags.Static | global::System.Reflection.BindingFlags.NonPublic); harmony.Patch(originalPatch23f715ba229342e08d6fb11494a17f90, prefix: new HarmonyMethod(Patch23f715ba229342e08d6fb11494a17f90)); var originalPatch147ac8e5593f4d678d1855b2e81726e7 = typeof (global::MockData.MyClock).GetMethod( "GetUtcNow" , new Type[] { } ); var Patch147ac8e5593f4d678d1855b2e81726e7 = typeof (Patch147ac8e5593f4d678d1855b2e81726e7).GetMethod( "Prefix" , global::System.Reflection.BindingFlags.Static | global::System.Reflection.BindingFlags.NonPublic); harmony.Patch(originalPatch147ac8e5593f4d678d1855b2e81726e7, prefix: new HarmonyMethod(Patch147ac8e5593f4d678d1855b2e81726e7)); } } [global::System.CodeDom.Compiler.GeneratedCode( "MockMe" , "1.1.2.0" )] internal class MyClockMockSetup : global::MockMe.Mocks.ClassMemberMocks.Setup.MemberMockSetup { private global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime>? GetNow_BagStore; public global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime> GetNow() { return this .GetNow_BagStore ??= new ();; } private global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime>? GetUtcNow_BagStore; public global::MockMe.Mocks.ClassMemberMocks.MemberMock<global::System.DateTime> GetUtcNow() { return this .GetUtcNow_BagStore ??= new ();; } [global::System.CodeDom.Compiler.GeneratedCode( "MockMe" , "1.1.2.0" )] internal class MyClockMockCallTracker : MockCallTracker { private readonly MyClockMockSetup setup; public MyClockMockCallTracker(MyClockMockSetup setup) { this .setup = setup; } private int GetNow_CallStore; public global::System.DateTime GetNow() { this .GetNow_CallStore++; return MockCallTracker.CallMemberMock<global::System.DateTime>( this .setup.GetNow_BagStore); } private int GetUtcNow_CallStore; public global::System.DateTime GetUtcNow() { this .GetUtcNow_CallStore++; return MockCallTracker.CallMemberMock<global::System.DateTime>( this .setup.GetUtcNow_BagStore); } [global::System.CodeDom.Compiler.GeneratedCode( "MockMe" , "1.1.2.0" )] internal class MyClockMockAsserter : MockAsserter { private readonly MyClockMockSetup.MyClockMockCallTracker tracker; public MyClockMockAsserter(MyClockMockSetup.MyClockMockCallTracker tracker) { this .tracker = tracker; } public global::MockMe.Asserters.MemberAsserter GetNow() => new ( this .tracker.GetNow_CallStore); public global::MockMe.Asserters.MemberAsserter GetUtcNow() => new ( this .tracker.GetUtcNow_CallStore); } } } } #pragma warning restore |
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MockMe
Leave a Reply