Skip to content

Commit

Permalink
fix: replace moq (#18)
Browse files Browse the repository at this point in the history
* chore: change namespace of domain unit tests

* chore: removed moq and unneeded resource lines

* chore: remove unnecessary resource lines
  • Loading branch information
katharinegillis authored Apr 6, 2024
1 parent 7ff023d commit 0df2cc4
Show file tree
Hide file tree
Showing 64 changed files with 1,455 additions and 1,996 deletions.
4 changes: 2 additions & 2 deletions Application.Tests/Application.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="Moq" Version="4.20.70"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
Expand All @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="UnitTests\"/>
<Folder Include="Unit\"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using Domain.FeatureFlags;
using Application.Interactors.CreateFeatureFlag;
using Domain.Common;
using NSubstitute;

namespace Application.Tests.Unit.Interactors.CreateFeatureFlag;

[Category("Unit")]
public sealed class InteractorTests
{
[Test]
public void CreateFeatureFlagInteractor_Is_An_InputPort()
{
var repository = Substitute.For<IRepository>();
var factory = Substitute.For<IFactory>();

var interactor = new Interactor(repository, factory);

Assert.That(interactor, Is.InstanceOf<IInputPort>());
}

[Test]
public async Task CreateFeatureFlagInteractor_Creates_A_Feature_Flag()
{
var model = Substitute.For<IModel>();
model.Id.Returns("new_flag");
model.Enabled.Returns(true);
model.Validate().Returns(Result<bool, IEnumerable<ValidationError>>.Ok(true));

var repository = Substitute.For<IRepository>();
repository.Create(model).Returns(m => m.Arg<IModel>().Id);

var factory = Substitute.For<IFactory>();
factory.Create("new_flag", true).Returns(model);

var interactor = new Interactor(repository, factory);

var request = new RequestModel
{
Id = "new_flag",
Enabled = true
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().Ok();
}

[Test]
public async Task CreateFeatureFlagInteractor_Should_Return_Any_Validation_Errors()
{
var repository = Substitute.For<IRepository>();

var validationErrors = new ValidationError[]
{
new()
{
Field = "Id",
Message = "Some error"
},
new()
{
Field = "Id",
Message = "Some other error"
}
};

var model = Substitute.For<IModel>();
model.Id.Returns("new_flag");
model.Enabled.Returns(true);
model.Validate().Returns(Result<bool, IEnumerable<ValidationError>>.Err(validationErrors));

var factory = Substitute.For<IFactory>();
factory.Create("new_flag", true).Returns(model);

var interactor = new Interactor(repository, factory);

var request = new RequestModel
{
Id =
"new_flag",
Enabled = true
};
var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().BadRequest(validationErrors);
}

[Test]
public async Task
CreateFeatureFlagInteractor_Should_Return_Validation_Error_If_Id_Already_Used()
{
var validationError = new ValidationError
{
Field = "Id",
Message = "Id already exists"
};

var model = Substitute.For<IModel>();
model.Id.Returns("new_flag");
model.Enabled.Returns(true);
model.Validate().Returns(Result<bool, IEnumerable<ValidationError>>.Ok(true));

var repository = Substitute.For<IRepository>();
repository.Create(model).Returns(Result<string, Error>.Err(validationError));

var factory = Substitute.For<IFactory>();
factory.Create("new_flag", true).Returns(model);

var interactor = new Interactor(repository, factory);

var request = new RequestModel
{
Id = "new_flag",
Enabled = true
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

var validationErrors = new List<ValidationError> { validationError };
presenter.Received().BadRequest(Arg.Is<List<ValidationError>>(x => x.SequenceEqual(validationErrors)));
}

[Test]
public async Task CreateFeatureFlagInteractor_Should_Return_Error_If_Repository_Errors()
{
var error = new Error
{
Message = "Unknown error"
};

var model = Substitute.For<IModel>();
model.Id.Returns("new_flag");
model.Enabled.Returns(true);
model.Validate().Returns(Result<bool, IEnumerable<ValidationError>>.Ok(true));

var repository = Substitute.For<IRepository>();
repository.Create(model).Returns(Result<string, Error>.Err(error));

var factory = Substitute.For<IFactory>();
factory.Create("new_flag", true).Returns(model);

var interactor = new Interactor(repository, factory);

var request = new RequestModel
{
Id = "new_flag",
Enabled = true
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().Error(error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Application.Interactors.DeleteFeatureFlag;
using Domain.Common;
using Domain.FeatureFlags;
using NSubstitute;

namespace Application.Tests.Unit.Interactors.DeleteFeatureFlag;

[Category("Unit")]
public sealed class InteractorTests
{
[Test]
public void DeleteFeatureFlagInteractor_Is_An_InputPort()
{
var repository = Substitute.For<IRepository>();

var interactor = new Interactor(repository);

Assert.That(interactor, Is.InstanceOf<IInputPort>());
}

[Test]
public async Task DeleteFeatureFlagInteractor_Deletes_A_Feature_Flag()
{
var repository = Substitute.For<IRepository>();
repository.Delete("some_flag").Returns(Result<bool, Error>.Ok(true));

var interactor = new Interactor(repository);

var request = new RequestModel
{
Id = "some_flag"
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().Ok();
}

[Test]
public async Task DeleteFeatureFlagInteractor_Should_Return_NotFound_If_Flag_Not_Found()
{
var error = new NotFoundError();

var repository = Substitute.For<IRepository>();
repository.Delete("some_flag").Returns(Result<bool, Error>.Err(error));

var interactor = new Interactor(repository);

var request = new RequestModel
{
Id = "some_flag"
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().NotFound();
}

[Test]
public async Task DeleteFeatureFlagInteractor_Should_Return_Error_If_Repository_Errors()
{
var error = new Error
{
Message = "Unknown error"
};

var repository = Substitute.For<IRepository>();
repository.Delete("some_flag").Returns(Result<bool, Error>.Err(error));

var interactor = new Interactor(repository);

var request = new RequestModel
{
Id = "some_flag"
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().Error(error);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Application.Interactors.GetFeatureFlag;

namespace Application.Tests.UnitTests.Interactors.GetFeatureFlag;
namespace Application.Tests.Unit.Interactors.GetFeatureFlag;

[Category("Unit")]
public sealed class CodePresenterFactoryTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Application.Interactors.GetFeatureFlag;
using Domain.FeatureFlags;

namespace Application.Tests.UnitTests.Interactors.GetFeatureFlag;
namespace Application.Tests.Unit.Interactors.GetFeatureFlag;

[Category("Unit")]
public sealed class CodePresenterTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Application.Interactors.GetFeatureFlag;
using Domain.FeatureFlags;
using NSubstitute;

namespace Application.Tests.Unit.Interactors.GetFeatureFlag;

[Category("Unit")]
public sealed class InteractorTests
{
[Test]
public void GetFeatureFlagInteractor_Is_An_InputPort()
{
var repository = Substitute.For<IRepository>();

var interactor = new Interactor(repository);

Assert.That(interactor, Is.InstanceOf<IInputPort>());
}

[Test]
public async Task GetFeatureFlagInteractor_Returns_A_Feature_Flag()
{
var model = new Model
{
Id = "some_flag",
Enabled = true
};

var repository = Substitute.For<IRepository>();
repository.Get("some_flag").Returns(model);

var interactor = new Interactor(repository);

var request = new RequestModel
{
Id = "some_flag"
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().Ok(model);
}

[Test]
public async Task GetFeatureFlagInteractor_Returns_NotFound()
{
var repository = Substitute.For<IRepository>();
repository.Get("some_flag").Returns(new NullModel());

var interactor = new Interactor(repository);

var request = new RequestModel
{
Id = "some_flag"
};

var presenter = Substitute.For<IOutputPort>();

await interactor.Execute(request, presenter);

presenter.Received().NotFound();
}
}
Loading

0 comments on commit 0df2cc4

Please sign in to comment.