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

Posted: May 08, 2019 at 12:21 PM Quote #231365
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);
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: May 08, 2019 at 3:38 PM Quote #231386
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: May 13, 2019 at 7:54 AM Quote #231743
Thanks. Here is an issue to discuss.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: May 14, 2019 at 10:29 AM Quote #231820
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.