Skip to content

Commit f7f01e2

Browse files
committed
ShouldPassForThe now recognizes the model objects earlier in the assertions chain for views (#359)
1 parent 27cd04e commit f7f01e2

File tree

7 files changed

+92
-32
lines changed

7 files changed

+92
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void LoadPlugins(DependencyContext dependencyContext)
4545
var plugins = testFrameworkAssemblies
4646
.Select(l => Assembly
4747
.Load(new AssemblyName(l.Name))
48-
.GetType($"{testFrameworkName}.Plugins.{l.Name.Replace(testFrameworkName, string.Empty).Trim('.')}TestPlugin"))
48+
.GetType($"{testFrameworkName}.Plugins.{l.Name.Replace(testFrameworkName, string.Empty).Replace(".", string.Empty)}TestPlugin"))
4949
.Where(p => p != null)
5050
.ToArray();
5151

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace MyTested.AspNetCore.Mvc.Plugins
2+
{
3+
using System;
4+
using Internal.TestContexts;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
public class ControllersViewsTestPlugin : IDefaultRegistrationPlugin, IShouldPassForPlugin
9+
{
10+
public long Priority => -1000;
11+
12+
public Action<IServiceCollection> DefaultServiceRegistrationDelegate
13+
=> serviceCollection => serviceCollection
14+
.AddMvcCore()
15+
.AddFormatterMappings()
16+
.AddViews()
17+
.AddDataAnnotations();
18+
19+
public object TryGetValue(Type type, ComponentTestContext testContext)
20+
=> testContext.MethodResult switch
21+
{
22+
JsonResult jsonResult => jsonResult.Value,
23+
ViewResult viewResult => viewResult.Model,
24+
PartialViewResult partialViewResult => partialViewResult.Model,
25+
ViewComponentResult viewComponentResult => viewComponentResult.Model,
26+
_ => null
27+
};
28+
}
29+
}

src/MyTested.AspNetCore.Mvc.Controllers.Views/Plugins/ViewActionResultsTestPlugin.cs

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

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/BuildersTests/ActionResultsTests/JsonTests/JsonTestBuilderTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void AndAlsoShouldWorkCorrectly()
117117
}
118118

119119
[Fact]
120-
public void AndProvideTheActionResultShouldWorkCorrectly()
120+
public void ShouldPassForTheShouldWorkCorrectlyWithActionResult()
121121
{
122122
MyController<MvcController>
123123
.Instance()
@@ -132,6 +132,18 @@ public void AndProvideTheActionResultShouldWorkCorrectly()
132132
});
133133
}
134134

135+
[Fact]
136+
public void ShouldPassForTheShouldWorkCorrectlyWithModel()
137+
{
138+
MyController<MvcController>
139+
.Instance()
140+
.Calling(c => c.JsonAction())
141+
.ShouldReturn()
142+
.Json()
143+
.AndAlso()
144+
.ShouldPassForThe<ICollection<ResponseModel>>(model => model.Count == 2);
145+
}
146+
135147
[Fact]
136148
public void WithNullJsonShouldReturnNoModel()
137149
{

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/BuildersTests/ActionResultsTests/ViewTests/ViewComponentTestBuilderTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void AndAlsoShouldWorkCorrectly()
156156
}
157157

158158
[Fact]
159-
public void AndProvideTheActionResultShouldWorkCorrectly()
159+
public void ShouldPassForTheShouldWorkCorrectlyWithActionResult()
160160
{
161161
MyController<MvcController>
162162
.Instance()
@@ -171,15 +171,27 @@ public void AndProvideTheActionResultShouldWorkCorrectly()
171171
});
172172
}
173173

174+
[Fact]
175+
public void ShouldPassForTheShouldWorkCorrectlyWithModel()
176+
{
177+
MyController<MvcController>
178+
.Instance()
179+
.Calling(c => c.CustomViewComponentResultWithViewData())
180+
.ShouldReturn()
181+
.ViewComponent()
182+
.AndAlso()
183+
.ShouldPassForThe<ICollection<ResponseModel>>(model => model.Count == 2);
184+
}
185+
174186
[Fact]
175187
public void WithNoModelShouldNotThrowExceptionWhenNoModelIsReturned()
176188
{
177189
MyController<MvcController>
178190
.Instance()
179191
.Calling(c => c.ViewComponentResultByType())
180192
.ShouldReturn()
181-
.ViewComponent(viewComponent =>
182-
viewComponent.WithNoModel());
193+
.ViewComponent(viewComponent => viewComponent
194+
.WithNoModel());
183195
}
184196

185197
[Fact]
@@ -189,8 +201,8 @@ public void WithModelOfTypeShouldNotThrowExceptionWithCorrectTypeForViewComponen
189201
.Instance()
190202
.Calling(c => c.CustomViewComponentResultWithViewData())
191203
.ShouldReturn()
192-
.ViewComponent(viewComponent =>
193-
viewComponent.WithModelOfType<List<ResponseModel>>());
204+
.ViewComponent(viewComponent => viewComponent
205+
.WithModelOfType<List<ResponseModel>>());
194206
}
195207

196208
[Fact]

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/BuildersTests/ActionResultsTests/ViewTests/ViewTestBuilderTests.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public void AndAlsoShouldWorkCorrectlyForPartialsWhenDifferInStatusCode()
505505
}
506506

507507
[Fact]
508-
public void AndProvideTheActionResultShouldWorkCorrectly()
508+
public void ShouldPassForTheShouldWorkCorrectlyWithViewAndActionResult()
509509
{
510510
MyController<MvcController>
511511
.Instance()
@@ -521,7 +521,19 @@ public void AndProvideTheActionResultShouldWorkCorrectly()
521521
}
522522

523523
[Fact]
524-
public void AndProvideTheActionResultShouldWorkCorrectlyWithPartial()
524+
public void ShouldPassForTheShouldWorkCorrectlyWithViewAndModel()
525+
{
526+
MyController<MvcController>
527+
.Instance()
528+
.Calling(c => c.DefaultViewWithModel())
529+
.ShouldReturn()
530+
.View()
531+
.AndAlso()
532+
.ShouldPassForThe<ICollection<ResponseModel>>(model => model.Count == 2);
533+
}
534+
535+
[Fact]
536+
public void ShouldPassForTheShouldWorkCorrectlyWithPartialViewAndActionResult()
525537
{
526538
MyController<MvcController>
527539
.Instance()
@@ -536,15 +548,27 @@ public void AndProvideTheActionResultShouldWorkCorrectlyWithPartial()
536548
});
537549
}
538550

551+
[Fact]
552+
public void ShouldPassForTheShouldWorkCorrectlyWithPartialViewAndModel()
553+
{
554+
MyController<MvcController>
555+
.Instance()
556+
.Calling(c => c.DefaultPartialViewWithModel())
557+
.ShouldReturn()
558+
.PartialView()
559+
.AndAlso()
560+
.ShouldPassForThe<ICollection<ResponseModel>>(model => model.Count == 2);
561+
}
562+
539563
[Fact]
540564
public void WithModelOfTypeShouldNotThrowExceptionWithCorrectTypeForPartialView()
541565
{
542566
MyController<MvcController>
543567
.Instance()
544568
.Calling(c => c.CustomPartialViewResultWithViewData())
545569
.ShouldReturn()
546-
.PartialView(partialView =>
547-
partialView.WithModelOfType<List<ResponseModel>>());
570+
.PartialView(partialView => partialView
571+
.WithModelOfType<List<ResponseModel>>());
548572
}
549573

550574
[Fact]
@@ -585,7 +609,7 @@ public void AndProvideTheActionResultShouldWorkCorrectlyForPartialViewModel()
585609
.ShouldPassForThe<IActionResult>(actionResult =>
586610
{
587611
Assert.NotNull(actionResult);
588-
Assert.NotNull((actionResult as PartialViewResult).Model);
612+
Assert.NotNull((actionResult as PartialViewResult)?.Model);
589613
});
590614
}
591615

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/PluginTests/ViewActionResultTestPluginTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ViewActionResultTestPluginTest
1111
[Fact]
1212
public void ShouldHavePriorityWithDefaultValue()
1313
{
14-
var testPlugin = new ViewActionResultsTestPlugin();
14+
var testPlugin = new ControllersViewsTestPlugin();
1515

1616
Assert.IsAssignableFrom<IDefaultRegistrationPlugin>(testPlugin);
1717
Assert.NotNull(testPlugin);
@@ -21,15 +21,15 @@ public void ShouldHavePriorityWithDefaultValue()
2121
[Fact]
2222
public void ShouldThrowArgumentNullExceptionWithInvalidServiceCollection()
2323
{
24-
var testPlugin = new ViewActionResultsTestPlugin();
24+
var testPlugin = new ControllersViewsTestPlugin();
2525

2626
Assert.Throws<ArgumentNullException>(() => testPlugin.DefaultServiceRegistrationDelegate(null));
2727
}
2828

2929
[Fact]
3030
public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection()
3131
{
32-
var testPlugin = new ViewActionResultsTestPlugin();
32+
var testPlugin = new ControllersViewsTestPlugin();
3333
var serviceCollection = new ServiceCollection();
3434

3535
testPlugin.DefaultServiceRegistrationDelegate(serviceCollection);

0 commit comments

Comments
 (0)