Skip to content

Commit fcd1671

Browse files
committed
Fixed failing attribute assertion tests (#359)
1 parent de73109 commit fcd1671

8 files changed

Lines changed: 66 additions & 32 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AbstractionsTestPlugin : BaseTestPlugin, IServiceRegistrationPlugin
1313
public Action<IServiceCollection> ServiceRegistrationDelegate =>
1414
serviceCollection => serviceCollection.AddCoreTesting();
1515

16-
// Тhis call prepares all application conventions and fills the controller action descriptor cache.
16+
// This call prepares all application conventions and fills the controller action descriptor cache.
1717
public Action<IServiceProvider> InitializationDelegate => serviceProvider => serviceProvider.GetService<IControllerActionDescriptorCache>();
1818

1919
public object TryGetValue(Type type, ComponentTestContext testContext)

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Extensions/ObjectExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static T TryCastTo<T>(this object obj)
2020
}
2121
catch (InvalidCastException)
2222
{
23-
return default(T);
23+
return default;
2424
}
2525
}
2626

src/MyTested.AspNetCore.Mvc.Controllers.Views/Builders/ActionResults/Json/JsonTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public JsonTestBuilder(ControllerTestContext testContext)
3737

3838
public override object GetActualModel() => this.GetJsonResult()?.Value;
3939

40-
public JsonResult GetJsonResult() => this.TestContext.MethodResult as JsonResult;
40+
public JsonResult GetJsonResult() => this.TestContext.MethodResultAs<JsonResult>();
4141

4242
/// <summary>
4343
/// Throws new <see cref="JsonResultAssertionException"/> for the provided property name, expected value and actual value.

src/MyTested.AspNetCore.Mvc.Controllers.Views/Builders/ActionResults/View/BaseTestBuilderWithViewFeatureResult.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.ActionResults.View
22
{
3+
using System;
34
using Builders.Base;
45
using Contracts.Base;
56
using Internal.TestContexts;
@@ -47,18 +48,13 @@ protected BaseTestBuilderWithViewFeatureResult(
4748
public abstract TViewFeatureResultTestBuilder ResultTestBuilder { get; }
4849

4950
public override object GetActualModel()
50-
{
51-
if (this.ActionResult is ViewResult)
52-
{
53-
return (this.ActionResult as ViewResult).Model;
54-
}
55-
else if (this.ActionResult is PartialViewResult)
51+
=> this.ActionResult switch
5652
{
57-
return (this.ActionResult as PartialViewResult)?.ViewData?.Model;
58-
}
59-
60-
return (this.ActionResult as ViewComponentResult)?.ViewData?.Model;
61-
}
53+
ViewResult viewResult => viewResult.Model,
54+
PartialViewResult partialViewResult => partialViewResult.ViewData?.Model,
55+
ViewComponentResult viewComponentResult => viewComponentResult.ViewData?.Model,
56+
_ => this.ActionResult
57+
};
6258

6359
public override void ValidateNoModel()
6460
{

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Actions/ShouldReturn/ShouldReturnActionResultTestBuilder.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Utilities.Validators;
1010

1111
/// <summary>
12-
/// Used for testing returned <see cref="ActionResult"/>.
12+
/// Used for testing returned <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/>.
1313
/// </summary>
1414
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Core MVC controller.</typeparam>
1515
public class ShouldReturnActionResultTestBuilder<TActionResult>
@@ -29,16 +29,15 @@ public ShouldReturnActionResultTestBuilder(ControllerTestContext testContext)
2929
}
3030

3131
/// <inheritdoc />
32-
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult()
32+
public new IAndTestBuilder ActionResult()
3333
{
3434
this.ValidateActionResults();
3535

3636
return new AndTestBuilder(this.TestContext);
3737
}
3838

3939
/// <inheritdoc />
40-
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult(
41-
Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder)
40+
public new IAndTestBuilder ActionResult(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder)
4241
{
4342
this.ValidateActionResults();
4443

@@ -47,14 +46,14 @@ IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult
4746
return new AndTestBuilder(this.TestContext);
4847
}
4948

50-
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult<TResult>()
49+
public new IAndTestBuilder ActionResult<TResult>()
5150
{
5251
this.ValidateActionResult<TResult>();
5352

5453
return new AndTestBuilder(this.TestContext);
5554
}
5655

57-
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult<TResult>(
56+
public new IAndTestBuilder ActionResult<TResult>(
5857
Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder)
5958
{
6059
this.ValidateActionResult<TResult>();

test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/BuildersTests/AttributesTests/ActionAttributesTestBuilderTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrong
591591
.ActionAttributes(attributes => attributes
592592
.SpecifyingMiddleware(typeof(OtherPipeline)));
593593
},
594-
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
594+
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'OtherPipeline' type, but in fact found 'Pipeline'.");
595595
}
596596

597597
[Fact]
@@ -620,7 +620,7 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithWrongConfigurationType()
620620
.SpecifyingMiddleware(middleware => middleware
621621
.OfType(typeof(OtherPipeline))));
622622
},
623-
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
623+
"When calling VariousAttributesAction action in MvcController expected action to have MiddlewareFilterAttribute with 'OtherPipeline' type, but in fact found 'Pipeline'.");
624624
}
625625

626626
[Fact]
@@ -733,7 +733,7 @@ public void WithTypeFilterShouldThrowExceptionWithCorrectAttributeAndWrongImplem
733733
.ActionAttributes(attributes => attributes
734734
.WithTypeFilter(typeof(OtherActionFilterWithArgs)));
735735
},
736-
"When calling VariousAttributesAction action in MvcController expected action to have TypeFilterAttribute with 'MyOtherActionFilterWithArgs' type, but in fact found 'MyActionFilterWithArgs'.");
736+
"When calling VariousAttributesAction action in MvcController expected action to have TypeFilterAttribute with 'OtherActionFilterWithArgs' type, but in fact found 'CustomActionFilterWithArgs'.");
737737

738738
MyApplication.StartsFrom<DefaultStartup>();
739739
}
@@ -770,7 +770,7 @@ public void WithTypeFilterShouldThrowExceptionWithWrongImplementationType()
770770
.WithTypeFilter(filter => filter
771771
.OfType(typeof(OtherActionFilterWithArgs))));
772772
},
773-
"When calling VariousAttributesAction action in MvcController expected action to have TypeFilterAttribute with 'MyOtherActionFilterWithArgs' type, but in fact found 'MyActionFilterWithArgs'.");
773+
"When calling VariousAttributesAction action in MvcController expected action to have TypeFilterAttribute with 'OtherActionFilterWithArgs' type, but in fact found 'CustomActionFilterWithArgs'.");
774774

775775
MyApplication.StartsFrom<DefaultStartup>();
776776
}
@@ -989,7 +989,7 @@ public void WithServiceFilterShouldThrowExceptionWithCorrectAttributeAndWrongCon
989989
.ActionAttributes(attributes => attributes
990990
.WithServiceFilter(typeof(OtherActionFilter)));
991991
},
992-
"When calling VariousAttributesAction action in MvcController expected action to have ServiceFilterAttribute with 'MyOtherActionFilter' type, but in fact found 'MyActionFilter'.");
992+
"When calling VariousAttributesAction action in MvcController expected action to have ServiceFilterAttribute with 'OtherActionFilter' type, but in fact found 'CustomActionFilter'.");
993993

994994
MyApplication.StartsFrom<DefaultStartup>();
995995
}
@@ -1026,7 +1026,7 @@ public void WithServiceFilterShouldThrowExceptionWithWrongServiceFilterType()
10261026
.WithServiceFilter(filter => filter
10271027
.OfType(typeof(OtherActionFilter))));
10281028
},
1029-
"When calling VariousAttributesAction action in MvcController expected action to have ServiceFilterAttribute with 'MyOtherActionFilter' type, but in fact found 'MyActionFilter'.");
1029+
"When calling VariousAttributesAction action in MvcController expected action to have ServiceFilterAttribute with 'OtherActionFilter' type, but in fact found 'CustomActionFilter'.");
10301030

10311031
MyApplication.StartsFrom<DefaultStartup>();
10321032
}

test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/BuildersTests/AttributesTests/ControllerAttributesTestBuilderTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithCorrectAttributeAndWrong
473473
.Attributes(attributes => attributes
474474
.SpecifyingMiddleware(typeof(OtherPipeline)));
475475
},
476-
"When testing ApiController was expected to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
476+
"When testing ApiController was expected to have MiddlewareFilterAttribute with 'OtherPipeline' type, but in fact found 'Pipeline'.");
477477
}
478478

479479
[Fact]
@@ -498,7 +498,7 @@ public void SpecifyingMiddlewareShouldThrowExceptionWithWrongConfigurationType()
498498
.SpecifyingMiddleware(middleware => middleware
499499
.OfType(typeof(OtherPipeline))));
500500
},
501-
"When testing ApiController was expected to have MiddlewareFilterAttribute with 'MyOtherPipeline' type, but in fact found 'MyPipeline'.");
501+
"When testing ApiController was expected to have MiddlewareFilterAttribute with 'OtherPipeline' type, but in fact found 'Pipeline'.");
502502
}
503503

504504
[Fact]
@@ -597,7 +597,7 @@ public void WithTypeFilterShouldThrowExceptionWithCorrectAttributeAndWrongImplem
597597
.Attributes(attributes => attributes
598598
.WithTypeFilter(typeof(OtherActionFilterWithArgs)));
599599
},
600-
"When testing ApiController was expected to have TypeFilterAttribute with 'MyOtherActionFilterWithArgs' type, but in fact found 'MyActionFilterWithArgs'.");
600+
"When testing ApiController was expected to have TypeFilterAttribute with 'OtherActionFilterWithArgs' type, but in fact found 'CustomActionFilterWithArgs'.");
601601

602602
MyApplication.StartsFrom<DefaultStartup>();
603603
}
@@ -630,7 +630,7 @@ public void WithTypeFilterShouldThrowExceptionWithWrongImplementationType()
630630
.WithTypeFilter(filter => filter
631631
.OfType(typeof(OtherActionFilterWithArgs))));
632632
},
633-
"When testing ApiController was expected to have TypeFilterAttribute with 'MyOtherActionFilterWithArgs' type, but in fact found 'MyActionFilterWithArgs'.");
633+
"When testing ApiController was expected to have TypeFilterAttribute with 'OtherActionFilterWithArgs' type, but in fact found 'CustomActionFilterWithArgs'.");
634634

635635
MyApplication.StartsFrom<DefaultStartup>();
636636
}
@@ -827,7 +827,7 @@ public void WithServiceFilterShouldThrowExceptionWithCorrectAttributeAndWrongSer
827827
.Attributes(attributes => attributes
828828
.WithServiceFilter(typeof(OtherActionFilter)));
829829
},
830-
"When testing ApiController was expected to have ServiceFilterAttribute with 'MyOtherActionFilter' type, but in fact found 'MyActionFilter'.");
830+
"When testing ApiController was expected to have ServiceFilterAttribute with 'OtherActionFilter' type, but in fact found 'CustomActionFilter'.");
831831

832832
MyApplication.StartsFrom<DefaultStartup>();
833833
}
@@ -860,7 +860,7 @@ public void WithServiceFilterShouldThrowExceptionWithWrongServiceFilterType()
860860
.WithServiceFilter(filter => filter
861861
.OfType(typeof(OtherActionFilter))));
862862
},
863-
"When testing ApiController was expected to have ServiceFilterAttribute with 'MyOtherActionFilter' type, but in fact found 'MyActionFilter'.");
863+
"When testing ApiController was expected to have ServiceFilterAttribute with 'OtherActionFilter' type, but in fact found 'CustomActionFilter'.");
864864

865865
MyApplication.StartsFrom<DefaultStartup>();
866866
}

test/MyTested.AspNetCore.Mvc.Controllers.Test/BuildersTests/ActionsTests/ShouldReturnTests/ShouldReturnActionResultTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionsTests.ShouldReturnTests
22
{
33
using Exceptions;
4+
using Microsoft.AspNetCore.Mvc;
45
using Setups;
56
using Setups.Controllers;
67
using Setups.Models;
@@ -256,5 +257,43 @@ public void ShouldReturnActionResultOfTWithDetailsShouldThrowExceptionWhenResult
256257
},
257258
"When calling ActionResultOfT action in MvcController expected result to be ActionResult<RequestModel>, but instead received ActionResult<ResponseModel>.");
258259
}
260+
261+
[Fact]
262+
public void ShouldReturnActionResultShouldWorkCorrectlyWithShouldPassFotTheMethod()
263+
{
264+
MyController<MvcController>
265+
.Instance()
266+
.Calling(c => c.ActionResultInterface())
267+
.ShouldReturn()
268+
.ActionResult()
269+
.AndAlso()
270+
.ShouldPassForThe<OkObjectResult>(ok => ok
271+
.Value.GetType() == typeof(ResponseModel));
272+
}
273+
274+
[Fact]
275+
public void ShouldReturnActionResultOfTShouldWorkCorrectlyWithShouldPassFotTheMethodAndActionResult()
276+
{
277+
MyController<MvcController>
278+
.Instance()
279+
.Calling(c => c.ActionResultOfT(int.MaxValue))
280+
.ShouldReturn()
281+
.ActionResult()
282+
.AndAlso()
283+
.ShouldPassForThe<OkObjectResult>(ok => ok
284+
.Value.GetType() == typeof(ResponseModel));
285+
}
286+
287+
[Fact]
288+
public void ShouldReturnActionResultOfTShouldWorkCorrectlyWithShouldPassFotTheMethodAndModel()
289+
{
290+
MyController<MvcController>
291+
.Instance()
292+
.Calling(c => c.ActionResultOfT(1))
293+
.ShouldReturn()
294+
.ActionResult<ResponseModel>()
295+
.AndAlso()
296+
.ShouldPassForThe<ResponseModel>(model => model.IntegerValue == 1);
297+
}
259298
}
260299
}

0 commit comments

Comments
 (0)