Suggestion: Remove Nop.Tests/TestExtensions and replace with FluentAssertions.

1 year ago
We currently have the test class Nop.Tests/TestExtensions.cs that provides abstractions around Assert calls.

Whilst I agree that it is good to have such abstractions, there is currently a library called FluentAssertions which does this much better (see documentation).

All of the methods in the nopcommerce library have an equivalent in FluentAssertions.

If you would like me to raise this in github then please let me know.  Also, if we do go ahead with this then I would be happy to perform the implementation and provide a pull request.

I have provided the list of methods from the nopcommerce library below, followed by the FluentAssertions equivalent plus any additional notes.


// public static T ShouldNotNull<T>(this T obj)
username.Should().BeNull();
// Note: ShouldNotNull is inappropriately named as it asserts if null rather than if not null.  Fortunately, this method is not currently used.

// public static T ShouldNotNull<T>(this T obj, string message)
username.Should().BeNull("with message");
// Note: ShouldNotNull is inappropriately named as it asserts if null rather than if not null.  Fortunately, this method is not currently used.

// public static T ShouldNotBeNull<T>(this T obj)
username.Should().NotBeNull();

// public static T ShouldNotBeNull<T>(this T obj, string message)
username.Should().NotBeNull("with message");

// public static T ShouldEqual<T>(this T actual, object expected)
username.Should().Be("david");

// public static void ShouldEqual(this object actual, object expected, string message)
username.Should().Be("david", "with message");

// public static Exception ShouldBeThrownBy(this Type exceptionType, TestDelegate testDelegate)
subject.Invoking(x => x.Foo("Hello")).Should().Throw<InvalidOperationException>();
subject.Invoking(x => x.Foo("Hello")).Should().Throw<InvalidOperationException>().WithMessage("message");
subject.Invoking(x => x.Foo("Hello")).Should().Throw<InvalidOperationException>().WithInnerException<ArgumentNullException>();

// public static void ShouldBe<T>(this object actual)
username.Should().BeOfType<string>();

// public static void ShouldBeNull(this object actual)
username.Should().BeNull(); // same as above

// public static void ShouldBeTheSameAs(this object actual, object expected)
username.Should().BeSameAs(username);

// public static void ShouldBeNotBeTheSameAs(this object actual, object expected)
username.Should().NotBeSameAs(username);

// public static T CastTo<T>(this object source)
username = (string)obj;
// Notes:
// 1. This method is a cast and does not belong on a class that abstracts test assertions.
// 2. Therefore, there is no equivient method on FluentAssertions
// 3. In my opinion, we do not need an abstraction around a cast which is past of the C# language.
// 4. Fortunately, this method is not currently used.

// public static void ShouldBeTrue(this bool source)
isValid.Should().BeTrue();

// public static void ShouldBeFalse(this bool source)
isValid.Should().BeFalse();

// public static void AssertSameStringAs(this string actual, string expected)
username.Should().BeEquivalentTo("DAVID");
// Note: Both methods ignore string casing.

// public static T PropertiesShouldEqual<T>(this T actual, T expected, params string[] filters)
subject.Should().BeEquivalentTo(subjectDto);
subject.Should().BeEquivalentTo(subjectDto, opt => opt.Excluding(s => s.FliteredProperty);
1 year ago
Additionally, I have just noticed that Nop.Tests/TypeAssert is provides similar abstractions around assertions and could also probably be removed and replaced with FluentAssertions.
1 year ago
Thanks. Here is an issue to discuss.
1 year ago
RomanovM wrote:
Thanks. Here is an issue to discuss.


Thanks for creating this.  I have added further information and a PR to aid discussion on the github issue.  Thanks.