Valid8R 0.2.0
dotnet add package Valid8R --version 0.2.0
NuGet\Install-Package Valid8R -Version 0.2.0
<PackageReference Include="Valid8R" Version="0.2.0" />
paket add Valid8R --version 0.2.0
#r "nuget: Valid8R, 0.2.0"
// Install Valid8R as a Cake Addin #addin nuget:?package=Valid8R&version=0.2.0 // Install Valid8R as a Cake Tool #tool nuget:?package=Valid8R&version=0.2.0
Valid8R
Introduction
Valid8 is an extension library that provides functionality to assert and verify test data.
Code example
Currently in the standard we do the following to verify that a method has been called with the expected parameters:
this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(
SameExceptionAs(expectedSourceValidationException))),
Times.Once);
This will verify that the LogError
method was called with the expected exception, but it will not tell us what the reason was if the parameters were not as expected. We would only see:
Moq.MockException :
Expected invocation on the mock once, but was 0 times: broker => broker.LogError(It.Is<Xeption>(actualException => actualException.SameExceptionAs(expectedException)))
But thanks to a recent update to Xeptions that now exposes the reasons on a mismatch, we can now write the details to the test summary.
To do this, we need to make the following modifications to the test:
- Add the Valid8R nuget package to your project
- Add
ITestOutputHelper output
to your test constructor. This helper will allow us to write to the test summary in the Test Explorer window
public partial class SourceServiceTests
{
private readonly Mock<IStorageBroker> storageBrokerMock;
private readonly Mock<ILoggingBroker> loggingBrokerMock;
private readonly SourceService sourceService;
private readonly ITestOutputHelper output;
public SourceServiceTests(ITestOutputHelper output)
{
this.output = output;
this.storageBrokerMock = new Mock<IStorageBroker>();
this.loggingBrokerMock = new Mock<ILoggingBroker>();
this.sourceService = new SourceService(
storageBroker: this.storageBrokerMock.Object,
loggingBroker: this.loggingBrokerMock.Object);
}
. . .
}
- In the test we can now do the verification as follows:
output
refer to the helper we registered in the constructor abovereference
is a string that can be seen in the Test Explorer window. This can be useful to visually identify the thing that failed.
this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(
Valid8.SameExceptionAs(expectedSourceValidationException, output, "this.loggingBrokerMock.Verify"))),
Times.Once);
With this modification, if the parameters are not as expected, the test summary will now show the reason why the parameters were not as expected:
Moq.MockException :
Expected invocation on the mock once, but was 0 times: broker => broker.LogError(It.Is<Exception>(actualException => Valid8.IsSameExceptionAs(actualException, expectedException, testOutputHelper, reference)))
Performed invocations:
Mock<ILoggingBroker:1> (broker):
ILoggingBroker.LogError(GitFyle.Core.Api.Models.Foundations.Sources.Exceptions.SourceValidationException: Some validation error occurred, fix errors and try again.
---> GitFyle.Core.Api.Models.Foundations.Sources.Exceptions.NullSourceException: Source is null
at GitFyle.Core.Api.Services.Foundations.Sources.SourceService.ValidateSourceIsNotNull(Source source) in D:\Repos\STX\GitFyle.Core.Api\GitFyle.Core.Api\Services\Foundations\Sources\SourceService.Validations.cs:line 21
at GitFyle.Core.Api.Services.Foundations.Sources.SourceService.ValidateSourceOnAdd(Source source) in D:\Repos\STX\GitFyle.Core.Api\GitFyle.Core.Api\Services\Foundations\Sources\SourceService.Validations.cs:line 14
at GitFyle.Core.Api.Services.Foundations.Sources.SourceService.<>c__DisplayClass3_0.<<AddSourceAsync>b__0>d.MoveNext() in D:\Repos\STX\GitFyle.Core.Api\GitFyle.Core.Api\Services\Foundations\Sources\SourceService.cs:line 28
--- End of stack trace from previous location ---
at GitFyle.Core.Api.Services.Foundations.Sources.SourceService.TryCatch(ReturningSourceFunction returningSourceFunction) in D:\Repos\STX\GitFyle.Core.Api\GitFyle.Core.Api\Services\Foundations\Sources\SourceService.Exceptions.cs:line 20
--- End of inner exception stack trace ---)
Stack Trace:
Mock.Verify(Mock mock, LambdaExpression expression, Times times, String failMessage) line 331
Mock`1.Verify(Expression`1 expression, Times times) line 920
Mock`1.Verify(Expression`1 expression, Func`1 times) line 934
SourceServiceTests.ShouldThrowValidationExceptionIfSourceIsNullAndLogItAsync() line 43
--- End of stack trace from previous location ---
Standard Output:
this.loggingBrokerMock.Verify: Expected exception message to be "Source validation error occurred, fix errors and try again.", but found "Some validation error occurred, fix errors and try again.".
Valid8R also has a SameObjectAs<T>
method that can be used to verify that an object is the same as another object.
This can be useful when you want to verify that an object is the same as another object i.e. when you modify an object bases on some internal logic, but you never see the expected invocation.
this.storageBrokerMock.Verify(broker =>
broker.UpdateSourceAsync(
It.Is(Valid8.SameObjectAs<Source>(inputSource, output, "1st this.storageBrokerMock.Verify"))),
Times.Once);
Standard-Compliance
This library was built according to The Standard. The library follows engineering principles, patterns and tooling as recommended by The Standard.
This library is also a community effort which involves many hours of pair-programming, test-driven development and in-depth exploration, research, and design discussions.
Standard-Promise
The most important fulfillment aspect in a Standard complaint system is aimed towards contributing to people, its evolution, and principles. An organization that systematically honors an environment of learning, training, and sharing of knowledge is an organization that learns from the past, makes calculated risks for the future, and brings everyone within it up to speed on the current state of things as honestly, rapidly, and efficiently as possible.
We believe that everyone has the right to privacy, and will never do anything that could violate that right. We are committed to writing ethical and responsible software, and will always strive to use our skills, coding, and systems for the good. We believe that these beliefs will help to ensure that our software(s) are safe and secure and that it will never be used to harm or collect personal data for malicious purposes.
The Standard Community as a promise to you is in upholding these values.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- CompareNETObjects (>= 4.83.0)
- FluentAssertions (>= 6.12.0)
- Xeption (>= 2.7.0)
- xunit.abstractions (>= 2.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Alpha release of Valid8R