Skip to content

Commit

Permalink
Add Mocks.MockOptional to allow mocking optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cwinland committed Jun 13, 2024
1 parent 44cd58b commit d480d9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
9 changes: 7 additions & 2 deletions FastMoq.Core/Mocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Moq;
using Moq.Language.Flow;
using Moq.Protected;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
Expand Down Expand Up @@ -88,6 +87,12 @@ public class Mocker

#region Properties

/// <summary>
/// Gets the mock optional.
/// </summary>
/// <value>The mock optional.</value>
public bool MockOptional { get; set; } = false;

/// <summary>
/// Gets the database connection. The default value is a memory Sqlite database connection unless otherwise set.
/// </summary>
Expand Down Expand Up @@ -978,7 +983,7 @@ public DbContextMock<TDbContext> GetMockDbContext<TDbContext>() where TDbContext

try
{
return info.IsOptional ? null : GetParameter(info.ParameterType);
return !MockOptional && info.IsOptional ? null : GetParameter(info.ParameterType);
}
catch (FileNotFoundException ex)
{
Expand Down
2 changes: 1 addition & 1 deletion FastMoq.Core/MockerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public abstract partial class MockerTestBase<TComponent> : IDisposable where TCo
protected IEnumerable<MockModel> CustomMocks { get; set; } = new List<MockModel>();

/// <summary>
/// Gets or sets the create component action. This action is run whenever the component is created.
/// Gets or sets the creation component action. This action is run whenever the component is created.
/// </summary>
/// <value>The action to override the component creation.</value>
protected virtual Func<Mocker, TComponent> CreateComponentAction { get; }
Expand Down
4 changes: 3 additions & 1 deletion FastMoq.Tests/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using FastMoq.Tests.TestClasses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -217,6 +216,7 @@ public void CreateBest_Should_ThrowAmbiguous()
[Fact]
public void CreateClassWithInjectParameters()
{
Mocks.MockOptional = true;
var m = Mocks.CreateInstance<TestClassParameters>();
m.Should().NotBeNull();
m.anotherFileSystem.Should().NotBeNull();
Expand All @@ -226,6 +226,8 @@ public void CreateClassWithInjectParameters()
m.invalidInjection2.Should().BeNull();
m.invalidInjection3.Should().BeEmpty();
m.invalidInjection4.Should().BeEmpty();
m.fileSystem.Should().NotBeNull();
m.logger.Should().NotBeNull();

Mocks.CreateInstance<ITestClassOne>().Should().NotBeNull();
(Mocks.CreateInstance<ITestClassOne>() as TestClassOne).FileSystem.Should().NotBeNull();
Expand Down
11 changes: 9 additions & 2 deletions FastMoq.Tests/TestClasses/TestClassParameters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.IO.Abstractions;

#pragma warning disable CS8604 // Possible null reference argument for parameter.
Expand All @@ -18,6 +19,9 @@ public class InjectAttribute : Attribute

internal class TestClassParameters
{
internal readonly IFileSystem fileSystem;
internal readonly ILogger logger;

[Inject] internal IFileSystem anotherFileSystem;

[Inject] internal IFileSystem anotherFileSystem2 { get; set; }
Expand All @@ -32,12 +36,15 @@ internal class TestClassParameters

[Inject] internal string? invalidInjection4;

internal TestClassParameters(int x, string y, IFileSystem fileSystem)
internal TestClassParameters(int x, string y, IFileSystem? fileSystem, ILogger? logger = null)
{
if (fileSystem == null)
{
throw new ArgumentNullException(nameof(fileSystem));
}

this.fileSystem = fileSystem;
this.logger = logger;
}
}
}

0 comments on commit d480d9a

Please sign in to comment.