Skip to content

Commit 1fdba4e

Browse files
authored
Merge pull request #4 from YarinOmesi/dev
merge dev into master
2 parents 2318326 + 7908d33 commit 1fdba4e

37 files changed

Lines changed: 1452 additions & 17 deletions

.github/workflows/CI.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Build
2+
3+
on:
4+
workflow_call:
5+
push:
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Set Up .NET
14+
uses: actions/setup-dotnet@v2
15+
with:
16+
dotnet-version: 6.0.x
17+
- name: Restore
18+
run: dotnet restore
19+
- name: Build
20+
run: dotnet build -c Release --no-restore
21+
- name: Test
22+
run: dotnet test -c Release --no-build --verbosity normal
23+
- name: Pack
24+
run: dotnet pack -c Release --no-build -o nupkgs
25+
- uses: actions/cache@v2
26+
id: restore-build
27+
with:
28+
path: ./*
29+
key: ${{ github.sha }}
30+

.github/workflows/Release.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "*"
7+
8+
jobs:
9+
build:
10+
uses: ./.github/workflows/CI.yml
11+
release:
12+
needs: [build]
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/cache@v3
16+
id: restore-build
17+
with:
18+
path: ./*
19+
key: ${{ github.sha }}
20+
- name: Create GitHub release
21+
uses: softprops/action-gh-release@v1
22+
with:
23+
files: |
24+
${{ github.workspace }}/nupkgs/*.nupkg
25+
tag_name: ${{ steps.tagName.outputs.tag }}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Common IntelliJ Platform excludes
22

3-
3+
**/nupkg
44
**/.idea
55

66
# User specific

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Mock Filler
2+
3+
Creating tested class instance with mocks!
4+
5+
refer to [Test File Example](./Sample.Tests/Test.cs) to see an example
6+
7+
## How To Use
8+
9+
All you need to do is to mark you test fixture class as `partial`.
10+
11+
Create field of the desired tested class and mark it with attribute `[FillMocks]`.
12+
13+
The Source Generator will fill mocks field and create a `Build()` method to create the instance.
14+
15+
### Features
16+
17+
#### FillMocks
18+
19+
To Fill Mocks for given tested class, create a field for that class and mark it with `[FillMocks]`
20+
21+
#### Default Value
22+
23+
To declare a default value instead of creating a mock, create a field with name as following:
24+
25+
defaultValue[Constructor Parameter Name]
26+
27+
> you can add underscore and change the casing
28+
29+
##### Demonstration
30+
31+
```csharp
32+
private ILoggerFactory _defaultValueFactory = NullLoggerFactory.Instance;
33+
```
34+
35+
### Example
36+
37+
For This Code
38+
39+
```csharp
40+
// Class Being Tested
41+
public class TestedClass
42+
{
43+
private ILogger _logger;
44+
45+
public TestedClass(ILoggerFactory factory)
46+
{
47+
/* Some Code */
48+
}
49+
}
50+
51+
// Test Fixture class
52+
public partial class MyTestFixture
53+
{
54+
[FillMocks]
55+
private TestedClass _testedClasss;
56+
57+
/* Rest Of The Class */
58+
}
59+
```
60+
61+
The Generated Code Will Be
62+
63+
```csharp
64+
public partial class MyTestFixture
65+
{
66+
private Mock<ILoggerFactory> _loggerFactoryMock;
67+
68+
private TestedClass Build()
69+
{
70+
return new TestedClass(_loggerFactoryMock.Object);
71+
}
72+
}
73+
```

Sample.Tests/IDependency.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Sample.Tests;
2+
3+
public interface IDependency
4+
{
5+
6+
}

Sample.Tests/Sample.Tests.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
13+
<PackageReference Include="Moq" Version="4.18.4" />
14+
<PackageReference Include="NUnit" Version="3.13.3" />
15+
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\TestsHelper.SourceGenerator.Attributes\TestsHelper.SourceGenerator.Attributes.csproj" />
20+
<ProjectReference Include="..\TestsHelper.SourceGenerator\TestsHelper.SourceGenerator.csproj" PrivateAssets="all" ReferenceOutputAssembly="true" OutputItemType="Analyzer" />
21+
</ItemGroup>
22+
23+
24+
</Project>

Sample.Tests/Test.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.Logging.Abstractions;
3+
using NUnit.Framework;
4+
using TestsHelper.SourceGenerator.Attributes;
5+
6+
namespace Sample.Tests;
7+
8+
[TestFixture]
9+
public partial class Test
10+
{
11+
[FillMocks]
12+
private TestedClass _testedClass;
13+
14+
15+
private readonly ILoggerFactory _defaultValueFactory = NullLoggerFactory.Instance;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
_testedClass = Build();
21+
}
22+
23+
[Test]
24+
public void METHOD_On_Effect()
25+
{
26+
// Arrange
27+
28+
// Act
29+
_testedClass.VeryComplicatedLogic();
30+
// Assert
31+
32+
}
33+
}
34+

Sample.Tests/TestedClass.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace Sample.Tests;
4+
5+
public class TestedClass
6+
{
7+
private IDependency _dependency;
8+
private ILogger _logger;
9+
10+
public TestedClass(IDependency dependency, ILoggerFactory factory)
11+
{
12+
_dependency = dependency;
13+
_logger = factory.CreateLogger<TestedClass>();
14+
}
15+
16+
public int VeryComplicatedLogic()
17+
{
18+
return 0;
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace TestsHelper.SourceGenerator.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
6+
public class FillMocksAttribute : Attribute
7+
{
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<Version>1.0.0</Version>
10+
</PropertyGroup>
11+
</Project>

0 commit comments

Comments
 (0)