Skip to content

Commit e99b1d7

Browse files
committed
Removed assert helpers that conflict with Shoudly
1 parent 26be467 commit e99b1d7

6 files changed

Lines changed: 83 additions & 145 deletions

File tree

sample/tests/Tests/Components/TodoListTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public void Test005()
119119
cut.Find("input").Change(taskValue);
120120
cut.Find("form").Submit();
121121

122-
createdTask.ShouldNotBeNull();
123-
createdTask?.Text.ShouldBe(taskValue);
122+
createdTask = createdTask.ShouldBeOfType<Todo>();
123+
createdTask.Text.ShouldBe(taskValue);
124124
}
125125

126126
[Fact(DisplayName = "When add task form is submitted with no text OnAddingTodo is not called")]

src/Asserting/CollectionAssertExtensions.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,11 @@
77

88
namespace Egil.RazorComponents.Testing.Asserting
99
{
10-
/// <summary>
11-
/// Collection test assertions
12-
/// </summary>
13-
public static class GenericAssertExtensions
14-
{
15-
/// <summary>
16-
/// Verifies that <paramref name="actual"/> is not null
17-
/// and returns <paramref name="actual"/> again.
18-
/// </summary>
19-
/// <returns>Returns <paramref name="actual"/> if it is not null.</returns>
20-
public static T ShouldNotBeNull<T>([NotNullIfNotNull("actual")]this T? actual) where T : class
21-
{
22-
if (actual is null)
23-
throw new XunitException($"{nameof(ShouldNotBeNull)}() Failure");
24-
return actual;
25-
}
26-
27-
/// <summary>
28-
/// Verifies that <paramref name="actual"/> is not null
29-
/// and returns <paramref name="actual"/> again.
30-
/// </summary>
31-
/// <returns>Returns <paramref name="actual"/> if it is not null.</returns>
32-
public static T ShouldNotBeNull<T>([NotNullIfNotNull("actual")]this T? actual) where T : struct
33-
{
34-
if (actual is null)
35-
throw new XunitException($"{nameof(ShouldNotBeNull)}() Failure");
36-
return actual.Value;
37-
}
38-
39-
/// <summary>
40-
/// Verifies that a nullable <paramref name="actual"/> is not null
41-
/// and of type <typeparamref name="T"/>.
42-
/// </summary>
43-
/// <returns>Returns <paramref name="actual"/> as <typeparamref name="T"/>.</returns>
44-
public static T ShouldBeOfType<T>([NotNullIfNotNull("actual")]this object? actual)
45-
{
46-
return Assert.IsType<T>(actual);
47-
}
48-
49-
/// <summary>
50-
/// Verifies that a non nullable struct is the same as its nullable counter part.
51-
/// </summary>
52-
public static void ShouldBe<T>(this T actual, T? expected)
53-
where T : struct
54-
{
55-
Assert.Equal(expected, actual);
56-
}
57-
}
58-
5910
/// <summary>
6011
/// Collection test assertions
6112
/// </summary>
6213
public static class CollectionAssertExtensions
6314
{
64-
65-
6615
/// <summary>
6716
/// Verifies that a collection contains exactly a given number of elements, which
6817
/// meet the criteria provided by the element inspectors.

src/Asserting/DiffAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static IDiff ShouldHaveSingleChange(this IReadOnlyList<IDiff> diffs)
3737
/// The total number of <see cref="IDiff"/> inspectors must exactly match the number of <see cref="IDiff"/>s in the collection</param>
3838
public static void ShouldHaveChanges(this IReadOnlyList<IDiff> diffs, params Action<IDiff>[] diffInspectors)
3939
{
40-
Assert.Collection(diffs, diffInspectors);
40+
CollectionAssertExtensions.ShouldAllBe(diffs, diffInspectors);
4141
}
4242

4343
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using AngleSharp.Diffing.Core;
7+
using Egil.RazorComponents.Testing.Diffing;
8+
using Moq;
9+
using Shouldly;
10+
using Xunit;
11+
using Xunit.Sdk;
12+
13+
namespace Egil.RazorComponents.Testing.Asserting
14+
{
15+
public class DiffAssertExtensionsTest
16+
{
17+
[Fact(DisplayName = "ShouldHaveSingleChange throws when input is null")]
18+
public void Test001()
19+
{
20+
IReadOnlyList<IDiff>? diffs = null;
21+
Exception? exception = null;
22+
23+
try
24+
{
25+
DiffAssertExtensions.ShouldHaveSingleChange(diffs!);
26+
}
27+
catch (Exception ex)
28+
{
29+
exception = ex;
30+
};
31+
32+
exception.ShouldBeOfType<ArgumentNullException>();
33+
}
34+
35+
[Theory(DisplayName = "ShouldHaveSingleChange throws when input length not exactly 1")]
36+
[MemberData(nameof(GetDiffLists))]
37+
public void Test002(IReadOnlyList<IDiff> diffs)
38+
{
39+
Exception? exception = null;
40+
41+
try
42+
{
43+
diffs.ShouldHaveSingleChange();
44+
}
45+
catch (Exception ex)
46+
{
47+
exception = ex;
48+
};
49+
50+
exception.ShouldBeOfType<EqualException>();
51+
}
52+
53+
[Fact(DisplayName = "ShouldHaveSingleChange returns the single diff in input when there is only one")]
54+
public void Test003()
55+
{
56+
var input = new IDiff[] { Mock.Of<IDiff>() };
57+
58+
var output = input.ShouldHaveSingleChange();
59+
60+
output.ShouldBe(input[0]);
61+
}
62+
63+
internal static IEnumerable<object[]> GetDiffLists()
64+
{
65+
yield return new object[] { Array.Empty<IDiff>() };
66+
yield return new object[]
67+
{
68+
new IDiff[]
69+
{
70+
Mock.Of<IDiff>(),
71+
Mock.Of<IDiff>(),
72+
}
73+
};
74+
}
75+
}
76+
}

tests/Asserting/GenericAssertExtensions.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

tests/Asserting/GenericCollectionAssertExtensionsTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void Test001()
2828
exception = ex;
2929
};
3030

31-
var actual = exception.ShouldNotBeNull().ShouldBeOfType<CollectionException>();
31+
var actual = exception.ShouldBeOfType<CollectionException>();
3232
actual.ActualCount.ShouldBe(collection.Length);
3333
actual.ExpectedCount.ShouldBe(1);
3434
}
@@ -49,7 +49,7 @@ public void Test002()
4949
exception = ex;
5050
};
5151

52-
var actual = exception.ShouldNotBeNull().ShouldBeOfType<CollectionException>();
52+
var actual = exception.ShouldBeOfType<CollectionException>();
5353
actual.IndexFailurePoint.ShouldBe(1);
5454
}
5555

@@ -70,7 +70,7 @@ public void Test003()
7070
exception = ex;
7171
};
7272

73-
var actual = exception.ShouldNotBeNull().ShouldBeOfType<CollectionException>();
73+
var actual = exception.ShouldBeOfType<CollectionException>();
7474
actual.ActualCount.ShouldBe(collection.Length);
7575
actual.ExpectedCount.ShouldBe(1);
7676
}
@@ -91,7 +91,7 @@ public void Test004()
9191
exception = ex;
9292
};
9393

94-
var actual = exception.ShouldNotBeNull().ShouldBeOfType<CollectionException>();
94+
var actual = exception.ShouldBeOfType<CollectionException>();
9595
actual.IndexFailurePoint.ShouldBe(1);
9696
}
9797

0 commit comments

Comments
 (0)