Skip to content

Commit 74317c6

Browse files
committed
Improve error messages on ContainsItem authentication result assertions.
1 parent 34d32bb commit 74317c6

9 files changed

+142
-64
lines changed

src/FluentAssertions.AspNetCore.Mvc/ChallengeResultAssertions.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,8 @@ public ChallengeResultAssertions WithAllowRefresh(bool? expectedAllowRefresh, st
211211
public ChallengeResultAssertions ContainsItem(string expectedKey, string expectedValue, string reason = "", params object[] reasonArgs)
212212
{
213213
var actualItems = Items;
214-
var keyValuePair = new KeyValuePair<string, string>(expectedKey, expectedValue);
215214

216-
Execute.Assertion
217-
.ForCondition(actualItems.Contains(keyValuePair))
218-
.BecauseOf(reason, reasonArgs)
219-
.FailWith(string.Format(FailureMessages.CommonItemsContain, expectedKey, expectedValue));
215+
AssertionHelpers.AssertStringObjectDictionary(actualItems, "ChallengeResult.AuthenticationProperties.Items", expectedKey, expectedValue, reason, reasonArgs);
220216

221217
return this;
222218
}

src/FluentAssertions.AspNetCore.Mvc/ForbidResultAssertions.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
25
using System.Collections.Generic;
36
using System.Diagnostics;
4-
using System.Globalization;
57
using System.Linq;
6-
using FluentAssertions.Execution;
7-
using FluentAssertions.Primitives;
8-
using Microsoft.AspNetCore.Mvc;
98
using AuthenticationProperties = Microsoft.AspNetCore.Authentication.AuthenticationProperties;
109

1110
namespace FluentAssertions.AspNetCore.Mvc
@@ -22,7 +21,7 @@ public class ForbidResultAssertions : ObjectAssertions
2221
/// Initializes a new instance of the <see cref="T:ForbidResultAssertions" /> class.
2322
/// </summary>
2423
/// <param name="subject">The object to test assertion on</param>
25-
public ForbidResultAssertions(object subject) : base(subject)
24+
public ForbidResultAssertions(ForbidResult subject) : base(subject)
2625
{
2726
}
2827

@@ -211,12 +210,8 @@ public ForbidResultAssertions WithAllowRefresh(bool? expectedAllowRefresh, strin
211210
public ForbidResultAssertions ContainsItem(string expectedKey, string expectedValue, string reason = "", params object[] reasonArgs)
212211
{
213212
var actualItems = Items;
214-
var keyValuePair = new KeyValuePair<string, string>(expectedKey, expectedValue);
215213

216-
Execute.Assertion
217-
.ForCondition(actualItems.Contains(keyValuePair))
218-
.BecauseOf(reason, reasonArgs)
219-
.FailWith(string.Format(FailureMessages.CommonItemsContain, expectedKey, expectedValue));
214+
AssertionHelpers.AssertStringObjectDictionary(actualItems, "ForbidResult.AuthenticationProperties.Items", expectedKey, expectedValue, reason, reasonArgs);
220215

221216
return this;
222217
}

src/FluentAssertions.AspNetCore.Mvc/SignInResultAssertions.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
25
using System.Collections.Generic;
36
using System.Diagnostics;
4-
using System.Globalization;
57
using System.Security.Claims;
6-
using FluentAssertions.Execution;
7-
using FluentAssertions.Primitives;
8-
using Microsoft.AspNetCore.Mvc;
98
using AuthenticationProperties = Microsoft.AspNetCore.Authentication.AuthenticationProperties;
109

1110
namespace FluentAssertions.AspNetCore.Mvc
@@ -213,7 +212,7 @@ public SignInResultAssertions ContainsItem(string expectedKey, string expectedVa
213212
{
214213
var actualItems = Items;
215214

216-
AssertionHelpers.AssertStringObjectDictionary(actualItems, "SignInResult.Items", expectedKey, expectedValue, reason, reasonArgs);
215+
AssertionHelpers.AssertStringObjectDictionary(actualItems, "SignInResult.AuthenticationProperties.Items", expectedKey, expectedValue, reason, reasonArgs);
217216

218217
return this;
219218
}

src/FluentAssertions.AspNetCore.Mvc/SignOutResultAssertions.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
1+
using FluentAssertions.Execution;
2+
using FluentAssertions.Primitives;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
25
using System.Collections.Generic;
36
using System.Diagnostics;
4-
using System.Globalization;
57
using System.Linq;
6-
using FluentAssertions.Execution;
7-
using FluentAssertions.Primitives;
8-
using Microsoft.AspNetCore.Mvc;
98
using AuthenticationProperties = Microsoft.AspNetCore.Authentication.AuthenticationProperties;
109

1110
namespace FluentAssertions.AspNetCore.Mvc
@@ -210,12 +209,9 @@ public SignOutResultAssertions WithAllowRefresh(bool? expectedAllowRefresh, stri
210209
public SignOutResultAssertions ContainsItem(string expectedKey, string expectedValue, string reason = "", params object[] reasonArgs)
211210
{
212211
var actualItems = Items;
213-
var keyValuePair = new KeyValuePair<string, string>(expectedKey, expectedValue);
214212

215-
Execute.Assertion
216-
.ForCondition(actualItems.Contains(keyValuePair))
217-
.BecauseOf(reason, reasonArgs)
218-
.FailWith(string.Format(FailureMessages.CommonItemsContain, expectedKey, expectedValue));
213+
AssertionHelpers.AssertStringObjectDictionary(actualItems, "SignOutResult.AuthenticationProperties.Items",
214+
expectedKey, expectedValue, reason, reasonArgs);
219215

220216
return this;
221217
}

src/FluentAssertions.AspNetCore.Mvc/ViewResultAssertions.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using FluentAssertions.Common;
2-
using FluentAssertions.Execution;
1+
using FluentAssertions.Execution;
32
using FluentAssertions.Primitives;
43
using Microsoft.AspNetCore.Mvc;
54
using System;
6-
using System.Collections.Generic;
75
using System.Diagnostics;
86

97
namespace FluentAssertions.AspNetCore.Mvc

tests/FluentAssertions.AspNetCore.Mvc.Tests/ChallengeResultAssertions_Tests.cs

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Globalization;
4-
using FluentAssertions.Mvc.Tests.Helpers;
1+
using FluentAssertions.Mvc.Tests.Helpers;
52
using Microsoft.AspNetCore.Authentication;
63
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
77
using Xunit;
88

99
namespace FluentAssertions.AspNetCore.Mvc.Tests
@@ -276,18 +276,49 @@ public void ContainsItem_GivenExpected_ShouldPass()
276276
}
277277

278278
[Fact]
279-
public void ContainsItem_GivenUnexpected_ShouldFail()
279+
public void ContainsItem_GivenNull_ShouldFail()
280+
{
281+
const string testKey = "testKey";
282+
const string testValue = "testValue";
283+
ActionResult result = new ChallengeResult();
284+
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButFoundNull(
285+
"ChallengeResult.AuthenticationProperties.Items", testValue, testKey);
286+
287+
Action a = () => result.Should().BeChallengeResult().ContainsItem(testKey, testValue, Reason, ReasonArgs);
288+
289+
a.Should().Throw<Exception>().WithMessage(failureMessage);
290+
}
291+
292+
[Fact]
293+
public void ContainsItem_GivenUnexpectedKey_ShouldFail()
280294
{
281295
const string testKey = "testKey";
282296
const string testValue = "testValue";
283297
const string expectedKey = "wrong key";
298+
var properties = new Dictionary<string, string> { { testKey, testValue } };
299+
var actualAuthenticationProperties = new AuthenticationProperties(properties);
300+
ActionResult result = new ChallengeResult(actualAuthenticationProperties);
301+
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButKeyNotFound(
302+
"ChallengeResult.AuthenticationProperties.Items", testValue, expectedKey);
303+
304+
Action a = () => result.Should().BeChallengeResult().ContainsItem(expectedKey, testValue, Reason, ReasonArgs);
305+
306+
a.Should().Throw<Exception>().WithMessage(failureMessage);
307+
}
308+
309+
[Fact]
310+
public void ContainsItem_GivenUnexpectedValue_ShouldFail()
311+
{
312+
const string testKey = "testKey";
313+
const string testValue = "testValue";
284314
const string expectedValue = "wrong value";
285315
var properties = new Dictionary<string, string> { { testKey, testValue } };
286316
var actualAuthenticationProperties = new AuthenticationProperties(properties);
287317
ActionResult result = new ChallengeResult(actualAuthenticationProperties);
288-
var failureMessage = string.Format(FailureMessages.CommonItemsContain, expectedKey, expectedValue);
318+
var failureMessage = FailureMessageHelper.ExpectedAtKeyValueXButFoundY(
319+
"ChallengeResult.AuthenticationProperties.Items", testKey, expectedValue, testValue);
289320

290-
Action a = () => result.Should().BeChallengeResult().ContainsItem(expectedKey, expectedValue, Reason, ReasonArgs);
321+
Action a = () => result.Should().BeChallengeResult().ContainsItem(testKey, expectedValue, Reason, ReasonArgs);
291322

292323
a.Should().Throw<Exception>().WithMessage(failureMessage);
293324
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/ForbidResultAssertions_Tests.cs

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Globalization;
4-
using FluentAssertions.Mvc.Tests.Helpers;
1+
using FluentAssertions.Mvc.Tests.Helpers;
52
using Microsoft.AspNetCore.Authentication;
63
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
77
using Xunit;
88

99
namespace FluentAssertions.AspNetCore.Mvc.Tests
@@ -276,18 +276,49 @@ public void ContainsItem_GivenExpected_ShouldPass()
276276
}
277277

278278
[Fact]
279-
public void ContainsItem_GivenUnexpected_ShouldFail()
279+
public void ContainsItem_GivenNull_ShouldFail()
280+
{
281+
const string testKey = "testKey";
282+
const string testValue = "testValue";
283+
ActionResult result = new ForbidResult();
284+
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButFoundNull(
285+
"ForbidResult.AuthenticationProperties.Items", testValue, testKey);
286+
287+
Action a = () => result.Should().BeForbidResult().ContainsItem(testKey, testValue, Reason, ReasonArgs);
288+
289+
a.Should().Throw<Exception>().WithMessage(failureMessage);
290+
}
291+
292+
[Fact]
293+
public void ContainsItem_GivenUnexpectedKey_ShouldFail()
280294
{
281295
const string testKey = "testKey";
282296
const string testValue = "testValue";
283297
const string expectedKey = "wrong key";
298+
var properties = new Dictionary<string, string> { { testKey, testValue } };
299+
var actualAuthenticationProperties = new AuthenticationProperties(properties);
300+
ActionResult result = new ForbidResult(actualAuthenticationProperties);
301+
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButKeyNotFound(
302+
"ForbidResult.AuthenticationProperties.Items", testValue, expectedKey);
303+
304+
Action a = () => result.Should().BeForbidResult().ContainsItem(expectedKey, testValue, Reason, ReasonArgs);
305+
306+
a.Should().Throw<Exception>().WithMessage(failureMessage);
307+
}
308+
309+
[Fact]
310+
public void ContainsItem_GivenUnexpectedValue_ShouldFail()
311+
{
312+
const string testKey = "testKey";
313+
const string testValue = "testValue";
284314
const string expectedValue = "wrong value";
285315
var properties = new Dictionary<string, string> { { testKey, testValue } };
286316
var actualAuthenticationProperties = new AuthenticationProperties(properties);
287317
ActionResult result = new ForbidResult(actualAuthenticationProperties);
288-
var failureMessage = string.Format(FailureMessages.CommonItemsContain, expectedKey, expectedValue);
318+
var failureMessage = FailureMessageHelper.ExpectedAtKeyValueXButFoundY(
319+
"ForbidResult.AuthenticationProperties.Items", testKey, expectedValue, testValue);
289320

290-
Action a = () => result.Should().BeForbidResult().ContainsItem(expectedKey, expectedValue, Reason, ReasonArgs);
321+
Action a = () => result.Should().BeForbidResult().ContainsItem(testKey, expectedValue, Reason, ReasonArgs);
291322

292323
a.Should().Throw<Exception>().WithMessage(failureMessage);
293324
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/SignInResultAssertions_Tests.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using FluentAssertions.Mvc.Tests.Helpers;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
25
using System.Collections.Generic;
36
using System.Globalization;
47
using System.Security.Claims;
5-
using FluentAssertions.Mvc.Tests.Helpers;
6-
using Microsoft.AspNetCore.Authentication;
7-
using Microsoft.AspNetCore.Mvc;
88
using Xunit;
99

1010
namespace FluentAssertions.AspNetCore.Mvc.Tests
@@ -285,7 +285,7 @@ public void ContainsItem_GivenNull_ShouldFail()
285285
const string testValue = "testValue";
286286
ActionResult result = new SignInResult(TestAuthenticationScheme, TestClaimsPrincipal);
287287
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButFoundNull(
288-
"SignInResult.Items", testValue, testKey);
288+
"SignInResult.AuthenticationProperties.Items", testValue, testKey);
289289

290290
Action a = () => result.Should().BeSignInResult().ContainsItem(testKey, testValue, Reason, ReasonArgs);
291291

@@ -302,7 +302,7 @@ public void ContainsItem_GivenUnexpectedKey_ShouldFail()
302302
var actualAuthenticationProperties = new AuthenticationProperties(properties);
303303
ActionResult result = new SignInResult(TestAuthenticationScheme, TestClaimsPrincipal, actualAuthenticationProperties);
304304
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButKeyNotFound(
305-
"SignInResult.Items", testValue, expectedKey);
305+
"SignInResult.AuthenticationProperties.Items", testValue, expectedKey);
306306

307307
Action a = () => result.Should().BeSignInResult().ContainsItem(expectedKey, testValue, Reason, ReasonArgs);
308308

@@ -318,7 +318,8 @@ public void ContainsItem_GivenUnexpectedValue_ShouldFail()
318318
var properties = new Dictionary<string, string> { { testKey, testValue } };
319319
var actualAuthenticationProperties = new AuthenticationProperties(properties);
320320
ActionResult result = new SignInResult(TestAuthenticationScheme, TestClaimsPrincipal, actualAuthenticationProperties);
321-
var failureMessage = FailureMessageHelper.ExpectedAtKeyValueXButFoundY("SignInResult.Items", testKey, expectedValue, testValue);
321+
var failureMessage = FailureMessageHelper.ExpectedAtKeyValueXButFoundY(
322+
"SignInResult.AuthenticationProperties.Items", testKey, expectedValue, testValue);
322323

323324
Action a = () => result.Should().BeSignInResult().ContainsItem(testKey, expectedValue, Reason, ReasonArgs);
324325

tests/FluentAssertions.AspNetCore.Mvc.Tests/SignOutResultAssertions_Tests.cs

+39-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Globalization;
4-
using FluentAssertions.Mvc.Tests.Helpers;
1+
using FluentAssertions.Mvc.Tests.Helpers;
52
using Microsoft.AspNetCore.Authentication;
63
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
77
using Xunit;
88

99
namespace FluentAssertions.AspNetCore.Mvc.Tests
@@ -285,19 +285,50 @@ public void ContainsItem_GivenExpected_ShouldPass()
285285
}
286286

287287
[Fact]
288-
public void ContainsItem_GivenUnexpected_ShouldFail()
288+
public void ContainsItem_GivenNull_ShouldFail()
289+
{
290+
const string testKey = "testKey";
291+
const string testValue = "testValue";
292+
ActionResult result = new SignOutResult(TestAuthenticationSchemes);
293+
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButFoundNull(
294+
"SignOutResult.AuthenticationProperties.Items", testValue, testKey);
295+
296+
Action a = () => result.Should().BeSignOutResult().ContainsItem(testKey, testValue, Reason, ReasonArgs);
297+
298+
a.Should().Throw<Exception>().WithMessage(failureMessage);
299+
}
300+
301+
[Fact]
302+
public void ContainsItem_GivenUnexpectedKey_ShouldFail()
289303
{
290304
const string testKey = "testKey";
291305
const string testValue = "testValue";
292306
const string expectedKey = "wrong key";
293-
const string expectedValue = "wrong value";
307+
var properties = new Dictionary<string, string> { { testKey, testValue } };
308+
var actualAuthenticationProperties = new AuthenticationProperties(properties);
309+
ActionResult result = new SignOutResult(TestAuthenticationSchemes, actualAuthenticationProperties);
310+
var failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButKeyNotFound(
311+
"SignOutResult.AuthenticationProperties.Items", testValue, expectedKey);
312+
313+
Action a = () => result.Should().BeSignOutResult().ContainsItem(expectedKey, testValue, Reason, ReasonArgs);
314+
315+
a.Should().Throw<Exception>().WithMessage(failureMessage);
316+
}
294317

318+
[Fact]
319+
public void ContainsItem_GivenUnexpectedValue_ShouldFail()
320+
{
321+
const string testKey = "testKey";
322+
const string testValue = "testValue";
323+
const string expectedValue = "wrong value";
295324
var properties = new Dictionary<string, string> { { testKey, testValue } };
296325
var actualAuthenticationProperties = new AuthenticationProperties(properties);
297326
ActionResult result = new SignOutResult(TestAuthenticationSchemes, actualAuthenticationProperties);
298-
var failureMessage = string.Format(FailureMessages.CommonItemsContain, expectedKey, expectedValue);
327+
var failureMessage = FailureMessageHelper.ExpectedAtKeyValueXButFoundY(
328+
"SignOutResult.AuthenticationProperties.Items", testKey, expectedValue, testValue);
329+
330+
Action a = () => result.Should().BeSignOutResult().ContainsItem(testKey, expectedValue, Reason, ReasonArgs);
299331

300-
Action a = () => result.Should().BeSignOutResult().ContainsItem(expectedKey, expectedValue, Reason, ReasonArgs);
301332
a.Should().Throw<Exception>().WithMessage(failureMessage);
302333
}
303334

0 commit comments

Comments
 (0)