0

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 Answers1

0

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();
       }
 }

awright18
  • 2,255
  • 2
  • 23
  • 22
  • Thanks, but Shoudly already provides a ShouldSatisfyAllConditions function. My problem is finding any instance that there are two or more sequential asserts and refactor those into the ShouldSatisfyAllConditions block. – V. Rodrigues Oct 10 '19 at 13:43
  • sorry misread your question you want an analyzer to find those scenarios. My bad. – awright18 Oct 11 '19 at 15:23