Skip to content

Commit

Permalink
Add Property resolution; remove .net 5; Fix Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cwinland committed Mar 30, 2023
1 parent adf9a60 commit 5749b18
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 87 deletions.
14 changes: 1 addition & 13 deletions FastMoq.Core/FastMoq.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -38,14 +38,6 @@
<RootNamespace>FastMoq</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net5.0|AnyCPU'">
<DebugType>portable</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net5.0|AnyCPU'">
<DebugType>portable</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'">
<DebugType>portable</DebugType>
</PropertyGroup>
Expand Down Expand Up @@ -73,10 +65,6 @@
<Content Include="..\LICENSE.TXT" Link="license.txt" Pack="true" PackagePath="license.txt" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="5.*" />

</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="6.*" />

Expand Down
6 changes: 0 additions & 6 deletions FastMoq.Core/FastMoq.Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
<tags>Easy fast injection inject mock extension Moq moqthis framework mocking class</tags>
<repository type="git" url="https://github.com/cwinland/FastMoq" />
<dependencies>
<group targetFramework="net5.0">
<dependency id="Microsoft.Extensions.Http" version="5.0.0" exclude="Build,Analyzers" />
<dependency id="Moq" version="4.18.4" exclude="Build,Analyzers" />
<dependency id="NuGet.Versioning" version="6.4.0" exclude="Build,Analyzers" />
<dependency id="TestableIO.System.IO.Abstractions.TestingHelpers" version="19.1.5" exclude="Build,Analyzers" />
</group>
<group targetFramework="net6.0">
<dependency id="Microsoft.Extensions.Http" version="6.0.0" exclude="Build,Analyzers" />
<dependency id="Moq" version="4.18.4" exclude="Build,Analyzers" />
Expand Down
265 changes: 209 additions & 56 deletions FastMoq.Core/Mocker.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion FastMoq.Tests.Web/BlazorWeatherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void InjectedService_ShouldNotHaveInjectedParameter()
Setup();
Component = RenderComponent(true);
// Will be null because interface does not have inject attribute
Instance.WeatherService.FileSystem.Should().BeNull();
Instance.WeatherService.FileSystem.Should().NotBeNull();
}

[Fact]
Expand Down
49 changes: 45 additions & 4 deletions FastMoq.Tests.Web/MockerBlazorTestBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FastMoq.Tests.Blazor.Data;
using FastMoq.Tests.Blazor.Pages;
using FastMoq.Web.Blazor.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void AuthUser_Set_ShouldChangeUser()
AuthContext.IsAuthenticated.Should().BeTrue();
}

private void TestAuth<T>(Func<IEnumerable<T>> authCollection, ICollection<T> baseCollection, T newItem)
private static void TestAuth<T>(Func<IEnumerable<T>> authCollection, ICollection<T> baseCollection, T newItem)
{
authCollection.Invoke().Should().BeEquivalentTo(baseCollection.ToArray());
authCollection.Invoke().Should().HaveCount(0);
Expand All @@ -123,12 +124,52 @@ private void TestAuth<T>(Func<IEnumerable<T>> authCollection, ICollection<T> bas
}

[Fact]
public void AuthRoles_Set_ShouldChangeRoles() => TestAuth<string>(() => AuthContext.Roles, AuthorizedRoles, "testRole");
public void AuthRoles_Set_ShouldChangeRoles() => TestAuth(() => AuthContext.Roles, AuthorizedRoles, "testRole");

[Fact]
public void AuthClaims_Set_ShouldChangeClaims() => TestAuth<Claim>(() => AuthContext.Claims, AuthorizedClaims, new Claim("group", "testClaim"));
public void AuthClaims_Set_ShouldChangeClaims() => TestAuth(() => AuthContext.Claims, AuthorizedClaims, new Claim("group", "testClaim"));

[Fact]
public void AuthPolicies_Set_ShouldChange() => TestAuth<string>(() => AuthContext.Policies, AuthorizedPolicies, "testPolicy");
public void AuthPolicies_Set_ShouldChange() => TestAuth(() => AuthContext.Policies, AuthorizedPolicies, "testPolicy");

[Fact]
public void InterfaceProperties()
{
var obj = Mocks.GetObject<IHttpContextAccessor>();
obj.HttpContext.Should().NotBeNull();

var obj2 = Mocks.GetMock<IHttpContextAccessor>().Object;
obj2.HttpContext.Should().NotBeNull();
}

[Fact]
public void ClassProperties()
{
var obj = Mocks.CreateInstance<HttpContextAccessor>();
obj.HttpContext.Should().NotBeNull();

var obj2 = Mocks.CreateInstance<Microsoft.AspNetCore.Http.HttpContextAccessor>();
obj2.HttpContext.Should().NotBeNull();

var obj3 = Mocks.GetObject<HttpContextAccessor>();
obj3.HttpContext.Should().NotBeNull();

var obj4 = Mocks.GetMock<HttpContextAccessor>().Object;
obj4.HttpContext.Should().NotBeNull();
}

[Fact]
public void ClassProperties_NoResolution()
{
Mocks.InnerMockResolution = false;
var obj4 = Mocks.GetMock<HttpContextAccessor>().Object;
obj4.HttpContext.Should().BeNull();
}
}

public class HttpContextAccessor : IHttpContextAccessor
{
/// <inheritdoc />
public HttpContext? HttpContext { get; set; }
}
}
6 changes: 3 additions & 3 deletions FastMoq.Tests/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void CreateMockMappedObjectWithInjectParameters()
public void CreateMockObjectWithInjectParameters()
{
Mocks.GetObject<ITestClassOne>().Should().NotBeNull();
Mocks.GetObject<ITestClassOne>().FileSystem.Should().BeNull();
Mocks.GetObject<ITestClassOne>().FileSystem.Should().NotBeNull();
}

[Fact]
Expand Down Expand Up @@ -685,7 +685,7 @@ public void IsValidConstructor()
public void Mocker_CreateMockInstance_InnerMockResolution_False_ShouldThrow()
{
Component.InnerMockResolution = false;
new Action(() => Component.CreateMockInstance<TestClassMultiple>()).Should().Throw<ArgumentException>();
new Action(() => Component.CreateMockInstance<TestClassMultiple>()).Should().NotThrow<ArgumentException>();
}

[Fact]
Expand Down Expand Up @@ -773,7 +773,7 @@ private void CheckConstructorByArgs(object data, bool expected, bool nonPublic)
isValid.Should().Be(expected);
}

private void CheckTypes(IReadOnlyList<object?> argData, List<Type> types)
private static void CheckTypes(IReadOnlyList<object?> argData, List<Type> types)
{
for (var i = 0; i < argData.Count; i++)
{
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# [FastMoq](http://help.fastmoq.com/)

[http://help.fastmoq.com](http://help.fastmoq.com/)

Easy and fast extension of the [Moq](https://github.com/Moq) mocking framework for mocking and auto injection of classes when testing.

## API Documentation

[FastMoq API Documentation](https://cwinland.github.io/FastMoq/Help/html/N-FastMoq.htm)

## Features

- NOW BLAZOR SUPPORT in FastMoq and FastMoq.Web.
- Test without declaring Mocks (unless needed).
- Creates objects with chain of automatic injections in objects and their dependencies.
- Creates Mocks and Objects with properties populated.
- Automatically injects and creates components or services.
- Injection: Automatically determines what interfaces need to be injected into the constructor and creates mocks if they do not exist.
- Generate Mock using specific data.
- Best guess picks the multiple parameter constructor over the default constructor.
- Specific mapping allows the tester to create an instance using a specific constructor and specific data.
- Supports Inject Attributes and multiple constructors.
Expand All @@ -29,7 +33,7 @@ Easy and fast extension of the [Moq](https://github.com/Moq) mocking framework f

- .NET 7
- .NET 6
- .NET 5 (Deprecated. Remains in FastMoq.Core Only - Will be removed in future)
- ~~.NET 5~~ (Deprecated and removed)
- ~~.NET Core 3.1~~ (Deprecated and removed)

## Most used classes in the FastMoq namespace
Expand Down Expand Up @@ -128,11 +132,13 @@ Mocks.AddType<ITestClassDouble, TestClassDouble1>(() => new TestClassDouble());
[FastMoq API Documentation](https://cwinland.github.io/FastMoq/Help/html/N-FastMoq.htm)

## Breaking Change

- 2.23.Latest => Removed support for .NET Core 5.
- 2.22.1215 => Removed support for .NET Core 3.1 in FastMoq.Core. Deprecated .NET Core 5 and moved package supporting .NET Core 5.0 from FastMoq to FastMoq.Core.
- 1.22.810 => Removed setters on the MockerTestBase virtual methods: SetupMocksAction, CreateComponentAction, CreatedComponentAction
- 1.22.810 => Update Package Dependencies
- 1.22.728 => Initialize method will reset the mock, if it already exists. This is overridable by settings the reset parameter to false.
- 1.22.604 => Renamed Mocks to Mocker, Renamed TestBase to MockerTestBase.

## [License - MIT](./License)

[http://help.fastmoq.com](http://help.fastmoq.com/)

0 comments on commit 5749b18

Please sign in to comment.