Skip to content

Commit

Permalink
Added extension methods for cleaner arrange code. (#13)
Browse files Browse the repository at this point in the history
Added extension methods for cleaner arrange code
  • Loading branch information
hhoangnl authored May 18, 2021
1 parent 08bd029 commit d8d23c8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301
dotnet-version: 5.0.203

- name: Install dependencies
run: dotnet restore -s https://api.nuget.org/v3/index.json
Expand Down
2 changes: 1 addition & 1 deletion FluentArrange.NSubstitute.Tests/ArrangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Sut_Action_ShouldInvokeAction()
var invoked = false;

// Act
_ = Arrange.Sut<AccountService>(service => invoked = true);
_ = Arrange.Sut<AccountService>(_ => invoked = true);

// Assert
invoked.Should().BeTrue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions FluentArrange.NSubstitute/FluentArrange.NSubstitute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<TargetFramework>netstandard1.3</TargetFramework>
<LangVersion>8.0</LangVersion>
<Version>0.7.0-alpha</Version>
<Version>0.8</Version>
<Description>FluentArrange using NSubstitute as mocking framework.</Description>
<PackageProjectUrl>https://github.com/hhoangnl/FluentArrange</PackageProjectUrl>
<RepositoryUrl>https://github.com/hhoangnl/FluentArrange</RepositoryUrl>
<PackageReleaseNotes>Add a new signature for `WithDependency{T2}` to inject a `FluentArrangeContext{T}` into an Action</PackageReleaseNotes>
<PackageReleaseNotes>Added extension methods `Returns{T}(Action{T})` for cleaner arrange code.</PackageReleaseNotes>
<PackageTags>AAA TDD mocking unit-testing nsubstitute</PackageTags>
<Authors>Huy Hoang</Authors>
<Copyright>Huy Hoang</Copyright>
Expand Down
36 changes: 36 additions & 0 deletions FluentArrange.Tests/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Threading.Tasks;
using FluentArrange.Tests.TestClasses;
using FluentAssertions;
using Xunit;

namespace FluentArrange.Tests
{
public class ExtensionMethods
{
[Fact]
public void Returns_ActionT_ShouldInvokeAction()
{
// Arrange
Foo foo = new();

// Act
foo.Returns(e => e.Id = 1);

// Assert
foo.Id.Should().Be(1);
}

[Fact]
public void Returns_ActionTaskT_ShouldInvokeAction()
{
// Arrange
Task<Foo> foo = Task.FromResult(new Foo());

// Act
foo.Returns(e => e.Id = 2);

// Assert
foo.Result.Id.Should().Be(2);
}
}
}
4 changes: 2 additions & 2 deletions FluentArrange.Tests/FluentArrange.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions FluentArrange.Tests/TestClasses/Foo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace FluentArrange.Tests.TestClasses
{
public class Foo : IFoo
{
public int Id { get; set; }
}
}
18 changes: 18 additions & 0 deletions FluentArrange/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;

namespace FluentArrange
{
public static class ExtensionMethods
{
public static void Returns<T>(this Task<T> @object, Action<T> configure)
{
configure.Invoke(@object.GetAwaiter().GetResult());
}

public static void Returns<T>(this T @object, Action<T> configure)
{
configure.Invoke(@object);
}
}
}
4 changes: 2 additions & 2 deletions FluentArrange/FluentArrange.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<TargetFramework>netstandard1.3</TargetFramework>
<LangVersion>8.0</LangVersion>
<Version>0.7.0-alpha</Version>
<Version>0.8</Version>
<Description>Write clean Arrange code using Fluent syntax.</Description>
<PackageProjectUrl>https://github.com/hhoangnl/FluentArrange</PackageProjectUrl>
<RepositoryUrl>https://github.com/hhoangnl/FluentArrange</RepositoryUrl>
<PackageReleaseNotes>Add a new signature for `WithDependency{T2}` to inject a `FluentArrangeContext{T}` into an Action</PackageReleaseNotes>
<PackageReleaseNotes>Added extension methods `Returns{T}(Action{T})` for cleaner arrange code.</PackageReleaseNotes>
<PackageTags>AAA TDD mocking unit-testing</PackageTags>
<Authors>Huy Hoang</Authors>
<Copyright>Huy Hoang</Copyright>
Expand Down

0 comments on commit d8d23c8

Please sign in to comment.