Is there a Resharper pattern for sequential Shouldly asserts?

148 Views Asked by At

I'm trying to come up with a Resharper pattern to apply to sequential Shouldly asserts. For instance, I have these checks:

    field1.ShouldNotBeNull();
    field2.ShouldBe(expectedField2Value);

And in this case, it should be replaced with:

    this.ShouldSatisflyAllConditions(
    () => field1.ShouldNotBeNull(),
    () => field2.ShouldBe(expectedField2Value));

And there's no problem if this was the only case, but the thing is that there are a lot of different possibilities that aren't covered in the patterns I managed so far. What I'm trying to do is to get to the point where anytime I get two or more sequential checks of any kind (ShouldBeNull, ShouldNotBeNull, ShouldContain, etc) I'd be warned to put all of those inside a ShouldSatisfyAllConditions block, since if I keep it as individual asserts, the tests will stop running as soon as one failed, instead of giving me a list of failures in the latter case. The problems I'm facing are:

  1. I've been unable to use any field name in the pattern. When I select and "Search with Pattern", I just get the names of those specific fields, not applicable to the whole project.

EDIT: My mistake. The pattern I tried is applied anytime two fields with any name are asserted to not null.

  1. I've been unable to apply this with any of the Shouldly asserts and I have to put it as a case-by-case (such as the example above). I only get specific patterns instead of a more generic one.
  2. Besides the very specific cases, I can't apply this for anytime there's two or more cases. I have to select each pattern, 2 lines, 3 lines and so on.

I'm using VS 17 Enterprise and JetBrains ReSharper Ultimate 2019.2.2. This:

    $field1$.ShouldNotBeNull();
    $field2$.ShouldNotBeNull();

Replaced with this:

    this.ShouldSatisfyAllConditions(
() => $pageStyleProperty$.ShouldNotBeNull(),
    () => $_editorObject$.ShouldNotBeNull());
1

There are 1 best solutions below

2
awright18 On

If you really want to do this you can with a params Action[]

 public void ShouldSatisfyAllConditions(params Action[] assertions)
 {
       foreach (var assertion in assertions)
       {
           assertion?.Invoke();
       }
 }