Skip to content

Commit

Permalink
Renamed Mocks to Mocker, TestBase to MockerTestBase, MockModel return…
Browse files Browse the repository at this point in the history
…ed from AddMock
  • Loading branch information
cwinland committed Jun 4, 2022
1 parent 09620b2 commit 98e21eb
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 152 deletions.
29 changes: 9 additions & 20 deletions FastMoq.TestingExample/ExampleTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FluentAssertions;
using Moq;
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using Xunit;
Expand All @@ -9,10 +8,7 @@

namespace FastMoq.TestingExample
{
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
[SuppressMessage("ReSharper", "AccessToModifiedClosure")]
public class TestClassNormalTestsDefaultBase : TestBase<TestClassNormal>
public class TestClassNormalTestsDefaultBase : MockerTestBase<TestClassNormal>
{
[Fact]
public void Test1()
Expand All @@ -24,23 +20,19 @@ public void Test1()
}
}

[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
[SuppressMessage("ReSharper", "AccessToModifiedClosure")]
public class TestClassNormalTestsSetupBase : TestBase<TestClassNormal>
public class TestClassNormalTestsSetupBase : MockerTestBase<TestClassNormal>
{
public TestClassNormalTestsSetupBase() : base(SetupMocksAction) { }

private static void SetupMocksAction(Mocks mocks)
private static void SetupMocksAction(Mocker mocks)
{
var iFile = new FileSystem().File;
mocks.Strict = true;

mocks.Strict = true; // Indicates to use an empty mock instead of predefined IFileSystem (FileSystemMock).
mocks.Initialize<IFileSystem>(mock => mock.Setup(x => x.File).Returns(iFile));
}

[Fact]
public void Test1()
public void TestStrict()
{
Component.FileSystem.Should().NotBeNull();
Component.FileSystem.Should().NotBeOfType<MockFileSystem>();
Expand All @@ -49,21 +41,18 @@ public void Test1()
}
}

[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
[SuppressMessage("ReSharper", "AccessToModifiedClosure")]
public class TestClassNormalTestsFull : TestBase<TestClassNormal>
public class TestClassNormalTestsFull : MockerTestBase<TestClassNormal>
{
private static bool testEventCalled;
public TestClassNormalTestsFull() : base(SetupMocksAction, CreateComponentAction, CreatedComponentAction) => testEventCalled = false;
private static void CreatedComponentAction(TestClassNormal? obj) => obj.TestEvent += (_, _) => testEventCalled = true;
private static TestClassNormal CreateComponentAction(Mocks mocks) => new(mocks.GetObject<IFileSystem>());
private static TestClassNormal CreateComponentAction(Mocker mocks) => new(mocks.GetObject<IFileSystem>());

private static void SetupMocksAction(Mocks mocks)
private static void SetupMocksAction(Mocker mocks)
{
var mock = new Mock<IFileSystem>();
var iFile = new FileSystem().File;
mocks.Strict = true;
mocks.Strict = true; // Indicates to use an empty mock instead of predefined IFileSystem (FileSystemMock).
mocks.AddMock(mock, true);
mocks.Initialize<IFileSystem>(xMock => xMock.Setup(x => x.File).Returns(iFile));
}
Expand Down
4 changes: 2 additions & 2 deletions FastMoq.TestingExample/TestClassNormal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace FastMoq.TestingExample
{
public class TestClassNormal : ITestClassNormal
{
public event EventHandler TestEvent;
public IFileSystem FileSystem { get; set; }
public event EventHandler? TestEvent;
public IFileSystem? FileSystem { get; set; }
public TestClassNormal() { }
public TestClassNormal(IFileSystem fileSystem) => FileSystem = fileSystem;
public void CallTestEvent() => TestEvent?.Invoke(this, EventArgs.Empty);
Expand Down
5 changes: 3 additions & 2 deletions FastMoq.Tests/InstanceModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using FluentAssertions;
using Moq;
using System;
using System.IO.Abstractions;
using Xunit;
#pragma warning disable CS8602
#pragma warning disable CS8625

namespace FastMoq.Tests
{
public class InstanceModelTests : TestBase<InstanceModel<IFileSystem>>
public class InstanceModelTests : MockerTestBase<InstanceModel<IFileSystem>>
{
public InstanceModelTests() : base(mocks => new InstanceModel<IFileSystem>(mocks1 => new FileSystem()))
{
Expand Down
4 changes: 3 additions & 1 deletion FastMoq.Tests/MockModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using System;
using System.IO.Abstractions;
using Xunit;
#pragma warning disable CS8602
#pragma warning disable CS8625

namespace FastMoq.Tests
{
public class MockModelTests : TestBase<MockModel>
public class MockModelTests : MockerTestBase<MockModel>
{
public MockModelTests() : base(mocks => new MockModel(typeof(IFileSystem), new Mock<IFileSystem>()))
{
Expand Down
39 changes: 19 additions & 20 deletions FastMoq.Tests/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Reflection;
using System.Runtime;
using System.Security.Cryptography;
using Xunit;

#pragma warning disable CS8604
#pragma warning disable CS8602
#pragma warning disable CS8625

Expand All @@ -18,7 +17,7 @@ namespace FastMoq.Tests
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
[SuppressMessage("ReSharper", "AccessToModifiedClosure")]
public class MocksTests : TestBase<Mocks>
public class MocksTests : MockerTestBase<Mocker>
{
public MocksTests() : base(SetupAction, CreateAction, CreatedAction)
{
Expand Down Expand Up @@ -202,15 +201,15 @@ public void GetObject()
public void GetList()
{
var count = 0;
var numbers = Mocks.GetList(3, () => count++);
var numbers = Mocker.GetList(3, () => count++);
numbers.Should().BeEquivalentTo(new List<int> {0, 1, 2});

count = 0;
var strings = Mocks.GetList(3, () => (count++).ToString());
var strings = Mocker.GetList(3, () => (count++).ToString());
strings.Should().BeEquivalentTo(new List<string> { "0", "1", "2"});

count = 0;
var test = Mocks.GetList(3, () => new TestClassMany(count++));
var test = Mocker.GetList(3, () => new TestClassMany(count++));
test[0].value.Should().Be(0);
test[1].value.Should().Be(1);
test[2].value.Should().Be(2);
Expand All @@ -225,7 +224,6 @@ public void RemoveMock()
Mocks.RemoveMock(mock).Should().BeTrue();
Mocks.Contains<IFileSystemInfo>().Should().BeFalse();
Mocks.RemoveMock(mock).Should().BeFalse();

}

[Fact]
Expand All @@ -237,8 +235,9 @@ public void AddMock()
};

var mockResult = Mocks.AddMock(mock, false);
mockResult.Should().Be(mock);
mockResult.Name.Should().Be("First");
var mockModel = Mocks.GetMockModel<IFileSystemInfo>();
mockResult.Mock.Should().Be(mockModel.Mock);
mockModel.Mock.Name.Should().Be("First");
}

[Fact]
Expand All @@ -249,7 +248,7 @@ public void AddMockDuplicateWithoutOverwrite_ShouldThrowArgumentException()
Name = "First"
};

Mocks.AddMock(mock, false).Should().Be(mock);
Mocks.AddMock(mock, false).Mock.Should().Be(mock);

Action a = () => Mocks.AddMock(mock, false);
a.Should().Throw<ArgumentException>();
Expand All @@ -263,12 +262,12 @@ public void AddMockDuplicateWithOverwrite_ShouldSucceed()
Name = "First"
};

var mock1 = Mocks.AddMock(mock, false);
var mock1 = Mocks.AddMock(mock, false).Mock as Mock<IFileSystemInfo>;
mock1.Should().Be(mock);
mock1.Name.Should().Be("First");

mock.Name = "test";
var mock2 = Mocks.AddMock(mock, true);
var mock2 = Mocks.AddMock(mock, true).Mock as Mock<IFileSystemInfo>;
mock2.Should().Be(mock);
mock2.Name.Should().Be("test");

Expand Down Expand Up @@ -381,14 +380,14 @@ public void FindConstructorByBest()
private void CheckConstructorByArgs(object data, bool expected = true)
{
var constructor = Mocks.FindConstructor(typeof(TestClassNormal), data);
var isValid = Mocks.IsValidConstructor(constructor.Key, data);
var isValid = Mocker.IsValidConstructor(constructor.Key, data);
isValid.Should().Be(expected);
}

private void CheckBestConstructor(object data, bool expected = true)
{
var constructor = Mocks.FindConstructor(true, typeof(TestClassNormal));
var isValid = Mocks.IsValidConstructor(constructor.Key, data);
var isValid = Mocker.IsValidConstructor(constructor.Key, data);
isValid.Should().Be(expected);
}

Expand All @@ -410,21 +409,21 @@ public void FindConstructor_Exact()
public void IsValidConstructor()
{
var constructor = Mocks.FindConstructor(typeof(TestClassNormal), Mocks.GetObject<IFileSystem>());
var isValid = Mocks.IsValidConstructor(constructor.Key, Mocks.GetObject<IFileSystem>());
var isValid = Mocker.IsValidConstructor(constructor.Key, Mocks.GetObject<IFileSystem>());
isValid.Should().BeTrue();

isValid = Mocks.IsValidConstructor(constructor.Key, Mocks.GetObject<IFileSystem>(), 12);
isValid = Mocker.IsValidConstructor(constructor.Key, Mocks.GetObject<IFileSystem>(), 12);
isValid.Should().BeFalse();

isValid = Mocks.IsValidConstructor(constructor.Key, 12);
isValid = Mocker.IsValidConstructor(constructor.Key, 12);
isValid.Should().BeFalse();
}

private static Mocks CreateAction(Mocks mocks) => new();
private static Mocker CreateAction(Mocker mocks) => new();

private static void CreatedAction(Mocks? component) => component.Should().NotBeNull();
private static void CreatedAction(Mocker? component) => component.Should().NotBeNull();

private static void SetupAction(Mocks mocks)
private static void SetupAction(Mocker mocks)
{
mocks.Initialize<IDirectory>(mock => mock.SetupAllProperties());
mocks.Initialize<IFileInfo>(mock => mock.SetupAllProperties());
Expand Down
7 changes: 2 additions & 5 deletions FastMoq.Tests/NestedClassTests.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace FastMoq.Tests
{
public class NestedClassTests
{
private Mocks mocks;
private Mocker mocks;

public NestedClassTests()
{
mocks = new Mocks();
mocks = new Mocker();
}

[Fact]
Expand Down
4 changes: 3 additions & 1 deletion FastMoq.Tests/TestBaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using FluentAssertions;
using System;
using Xunit;
#pragma warning disable CS8604
#pragma warning disable CS8602

namespace FastMoq.Tests
{
public class TestBaseTests : TestBase<TestClass>
public class TestBaseTests : MockerTestBase<TestClass>
{
[Fact]
public void GetMember()
Expand Down
13 changes: 9 additions & 4 deletions FastMoq/FastMoq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>$(Authors)</Company>
<Copyright>Copyright(c) 2022 Christopher Winland</Copyright>
<PackageId>$(AssemblyName)</PackageId>
<Description>Easy and fast extension of moq (from moqthis), mocking framework, for mocking and auto injection of classes.</Description>
<Description>Easy and fast extension of the famous Moq mocking framework for mocking and auto injection of classes.</Description>
<PackageProjectUrl>https://github.com/cwinland/FastMoq</PackageProjectUrl>
<RepositoryUrl>https://github.com/cwinland/FastMoq</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down Expand Up @@ -44,13 +44,18 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.csHARP" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.csHARP" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.IO.Abstractions" Version="17.0.15" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="17.0.15" />

Expand Down
8 changes: 4 additions & 4 deletions FastMoq/InstanceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class InstanceModel<TClass> : InstanceModel where TClass : class
/// Gets or sets the create function.
/// </summary>
/// <value>The create function.</value>
public new Func<Mocks, TClass>? CreateFunc
public new Func<Mocker, TClass>? CreateFunc
{
get => (Func<Mocks, TClass>?) base.CreateFunc;
get => (Func<Mocker, TClass>?) base.CreateFunc;
set => base.CreateFunc = value;
}

Expand All @@ -35,7 +35,7 @@ public InstanceModel() : base(typeof(TClass))
/// Initializes a new instance of the <see cref="T:FastMoq.InstanceModel`1" /> class.
/// </summary>
/// <param name="createFunc">The create function.</param>
public InstanceModel(Func<Mocks, TClass>? createFunc) : this() => CreateFunc = createFunc;
public InstanceModel(Func<Mocker, TClass>? createFunc) : this() => CreateFunc = createFunc;
}

/// <summary>
Expand All @@ -57,7 +57,7 @@ public class InstanceModel
/// Gets or sets the create function.
/// </summary>
/// <value>The create function.</value>
public Func<Mocks, object>? CreateFunc { get; internal set; }
public Func<Mocker, object>? CreateFunc { get; internal set; }

#endregion

Expand Down
17 changes: 17 additions & 0 deletions FastMoq/MockModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

namespace FastMoq
{
public class MockModel<T> : MockModel where T : class
{
public new Mock<T> Mock
{
get => (Mock<T>)base.Mock;
set => base.Mock = value;
}

internal MockModel(Mock mock) : base(typeof(T), mock)
{
}

internal MockModel(MockModel mockModel) : base(mockModel.Type, mockModel.Mock)
{
}
}

/// <summary>
/// Class MockModel.
/// </summary>
Expand Down
Loading

0 comments on commit 98e21eb

Please sign in to comment.