Skip to content

Commit 6a5e11b

Browse files
committed
Added all generic ShouldPassForThe method options (#181)
1 parent 5d38882 commit 6a5e11b

File tree

48 files changed

+146
-820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+146
-820
lines changed

samples/MusicStore/MusicStore.Test/Controllers/ShoppingCartControllerTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using MyTested.AspNetCore.Mvc;
88
using ViewModels;
99
using Xunit;
10+
using Microsoft.AspNetCore.Http;
1011

1112
public class ShoppingCartControllerTest
1213
{
@@ -89,8 +90,7 @@ public void AddToCartShouldAddItemsToCart()
8990
.Redirect()
9091
.To<ShoppingCartController>(c => c.Index())
9192
.AndAlso()
92-
.ShouldPassFor()
93-
.TheHttpContext(async httpContext =>
93+
.ShouldPassForThe<HttpContext>(async httpContext =>
9494
{
9595
var cart = ShoppingCart.GetCart(From.Services<MusicStoreContext>(), httpContext);
9696
Assert.Equal(1, (await cart.GetCartItems()).Count);
@@ -127,8 +127,7 @@ public void RemoveFromCartShouldRemoveItemFromCart()
127127
Assert.Equal(" has been removed from your shopping cart.", model.Message);
128128
})
129129
.AndAlso()
130-
.ShouldPassFor()
131-
.TheHttpContext(async httpContext =>
130+
.ShouldPassForThe<HttpContext>(async httpContext =>
132131
{
133132
var cart = ShoppingCart.GetCart(From.Services<MusicStoreContext>(), httpContext);
134133
Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId));

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilder.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Base
22
{
33
using Contracts.Base;
4-
using Contracts.ShouldPassFor;
54
using Internal.TestContexts;
65
using Licensing;
76
using Microsoft.AspNetCore.Http;
8-
using ShouldPassFor;
9-
using System;
10-
using System.Reflection;
117
using Utilities.Validators;
128

139
/// <summary>
@@ -47,8 +43,5 @@ private set
4743
this.testContext = value;
4844
}
4945
}
50-
51-
/// <inheritdoc />
52-
public IShouldPassForTestBuilder ShouldPassFor() => new ShouldPassForTestBuilder(this.TestContext);
5346
}
5447
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Base
22
{
3-
using ShouldPassFor;
4-
53
/// <summary>
64
/// Base interface for all test builders.
75
/// </summary>
86
public interface IBaseTestBuilder
97
{
10-
/// <summary>
11-
/// Allows additional testing on various components.
12-
/// </summary>
13-
/// <returns>Test builder of <see cref="IShouldPassForTestBuilder"/> type.</returns>
14-
IShouldPassForTestBuilder ShouldPassFor();
158
}
169
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/ShouldPassFor/IShouldPassForTestBuilder.cs

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

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/ShouldPassFor/ShouldPassForTestBuilder.cs

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

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ public static void SetHttpContextToAccessor(HttpContext httpContext)
5252
public static TComponent TryGetShouldPassForValue<TComponent>(ComponentTestContext testContext)
5353
where TComponent : class
5454
{
55+
var result = testContext.ComponentAs<TComponent>()
56+
?? testContext.MethodResultAs<TComponent>()
57+
?? testContext.ModelAs<TComponent>();
58+
5559
foreach (var shouldPassForPlugin in ShouldPassForPlugins)
5660
{
57-
var result = shouldPassForPlugin.TryGetValue(typeof(TComponent), testContext) as TComponent;
5861
if (result != null)
5962
{
6063
return result;
6164
}
65+
66+
result = shouldPassForPlugin.TryGetValue(typeof(TComponent), testContext) as TComponent;
6267
}
6368

6469
throw new InvalidOperationException($"{typeof(TComponent).ToFriendlyTypeName()} could not be resolved for the 'ShouldPassForThe<TComponent>' method call.");

src/MyTested.AspNetCore.Mvc.Abstractions/Plugins/AbstractionsTestPlugin.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@ public class AbstractionsTestPlugin : BaseTestPlugin, IServiceRegistrationPlugin
1919
public Action<IServiceProvider> InitializationDelegate => serviceProvider => serviceProvider.GetService<IControllerActionDescriptorCache>();
2020

2121
public object TryGetValue(Type type, ComponentTestContext testContext)
22-
{
23-
if (Reflection.AreAssignable(baseHttpContextType, type))
24-
{
25-
return testContext.HttpContext;
26-
}
27-
28-
if (Reflection.AreAssignable(baseHttpRequestType, type))
29-
{
30-
return testContext.HttpRequest;
31-
}
32-
33-
return null;
34-
}
22+
=> Reflection.AreAssignable(baseHttpContextType, type) // HttpContext
23+
? testContext.HttpContext
24+
: Reflection.AreAssignable(baseHttpRequestType, type) // HttpRequest
25+
? (object)testContext.HttpRequest
26+
: null;
3527
}
3628
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace MyTested.AspNetCore.Mvc
2+
{
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using Utilities.Validators;
6+
7+
public class ActionAttributes : IEnumerable<object>
8+
{
9+
private readonly IEnumerable<object> attributes;
10+
11+
public ActionAttributes(IEnumerable<object> attributes)
12+
{
13+
CommonValidator.CheckForNullReference(attributes, nameof(ActionAttributes));
14+
this.attributes = attributes;
15+
}
16+
17+
public IEnumerator<object> GetEnumerator() => this.attributes.GetEnumerator();
18+
19+
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
20+
}
21+
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/ActionResults/BadRequest/BadRequestTestBuilder.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.ActionResults.BadRequest
22
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net;
36
using Base;
4-
using Contracts.Base;
57
using Contracts.ActionResults.BadRequest;
68
using Contracts.Models;
7-
using Contracts.ShouldPassFor;
89
using Exceptions;
910
using Internal.TestContexts;
1011
using Microsoft.AspNetCore.Mvc;
1112
using Microsoft.AspNetCore.Mvc.Formatters;
1213
using Microsoft.AspNetCore.Mvc.ModelBinding;
1314
using Microsoft.Net.Http.Headers;
14-
using Models;
15-
using System;
16-
using System.Collections.Generic;
17-
using System.Linq;
18-
using System.Net;
19-
using ShouldPassFor;
2015
using Utilities.Extensions;
2116

2217
/// <summary>
@@ -199,10 +194,7 @@ public IAndBadRequestTestBuilder WithErrorMessage(Func<string, bool> predicate)
199194

200195
/// <inheritdoc />
201196
public IBadRequestTestBuilder AndAlso() => this;
202-
203-
IShouldPassForTestBuilderWithActionResult<ActionResult> IBaseTestBuilderWithActionResult<ActionResult>.ShouldPassFor()
204-
=> new ShouldPassForTestBuilderWithActionResult<ActionResult>(this.TestContext);
205-
197+
206198
protected override void ThrowNewFailedValidationException(string propertyName, string expectedValue, string actualValue)
207199
=> this.ThrowNewHttpBadRequestResultAssertionException(propertyName, expectedValue, actualValue);
208200

src/MyTested.AspNetCore.Mvc.Controllers/Builders/ActionResults/Created/CreatedTestBuilder.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
using Base;
99
using Contracts.ActionResults.Created;
1010
using Contracts.Base;
11-
using Contracts.ShouldPassFor;
1211
using Contracts.Uri;
1312
using Exceptions;
1413
using Internal.TestContexts;
1514
using Microsoft.AspNetCore.Mvc;
1615
using Microsoft.AspNetCore.Mvc.Formatters;
1716
using Microsoft.AspNetCore.Routing;
1817
using Microsoft.Net.Http.Headers;
19-
using ShouldPassFor;
2018
using Utilities.Extensions;
2119
using Utilities.Validators;
2220

@@ -332,10 +330,7 @@ public IAndCreatedTestBuilder At<TController>(Expression<Func<TController, Task>
332330

333331
/// <inheritdoc />
334332
public ICreatedTestBuilder AndAlso() => this;
335-
336-
IShouldPassForTestBuilderWithActionResult<ObjectResult> IBaseTestBuilderWithActionResult<ObjectResult>.ShouldPassFor()
337-
=> new ShouldPassForTestBuilderWithActionResult<ObjectResult>(this.TestContext);
338-
333+
339334
/// <summary>
340335
/// Throws new created result assertion exception for the provided property name, expected value and actual value.
341336
/// </summary>

0 commit comments

Comments
 (0)