Skip to content

Commit fb15bfb

Browse files
authored
Merge pull request #348 from IvayloGoranov/development
Added direct lambda expression in the WithModelOfType<> option
2 parents f870a71 + 077918a commit fb15bfb

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

src/MyTested.AspNetCore.Mvc.Models/BaseTestBuilderWithResponseModelExtensions.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,56 @@ public static IAndModelDetailsTestBuilder<TModel> WithModelOfType<TModel>(
8989
return new ModelDetailsTestBuilder<TModel>(actualBuilder.TestContext);
9090
}
9191

92+
/// <summary>
93+
/// Tests whether model of the given type is returned from the invoked method and whether the provided predicate is passing.
94+
/// </summary>
95+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithResponseModel"/> type.</param>
96+
/// <typeparam name="TModel">Type of the model.</typeparam>
97+
/// <param name="predicate">Predicate testing the model.</param>
98+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TModel}"/>.</returns>
99+
public static IAndModelDetailsTestBuilder<TModel> WithModelOfType<TModel>(
100+
this IBaseTestBuilderWithResponseModel builder,
101+
Func<TModel, bool> predicate)
102+
{
103+
var actualBuilder = (BaseTestBuilderWithResponseModel)builder;
104+
105+
actualBuilder.WithModelOfType<TModel>();
106+
107+
if (!predicate(actualBuilder.TestContext.ModelAs<TModel>()))
108+
{
109+
throw new ResponseModelAssertionException(string.Format(
110+
"{0} response model {1} to pass the given predicate, but it failed.",
111+
actualBuilder.TestContext.ExceptionMessagePrefix,
112+
typeof(TModel).ToFriendlyTypeName()));
113+
}
114+
115+
actualBuilder.TestContext.Model = actualBuilder.GetActualModel<TModel>();
116+
117+
return new ModelDetailsTestBuilder<TModel>(actualBuilder.TestContext);
118+
}
119+
120+
/// <summary>
121+
/// Tests whether model of the given type is returned from the invoked method and whether the provided assertions are passing.
122+
/// </summary>
123+
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithResponseModel"/> type.</param>
124+
/// <typeparam name="TModel">Type of the model.</typeparam>
125+
/// <param name="assertions">Method containing all assertions for the model.</param>
126+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TModel}"/>.</returns>
127+
public static IAndModelDetailsTestBuilder<TModel> WithModelOfType<TModel>(
128+
this IBaseTestBuilderWithResponseModel builder,
129+
Action<TModel> assertions)
130+
{
131+
var actualBuilder = (BaseTestBuilderWithResponseModel)builder;
132+
133+
actualBuilder.WithModelOfType<TModel>();
134+
135+
assertions(actualBuilder.TestContext.ModelAs<TModel>());
136+
137+
actualBuilder.TestContext.Model = actualBuilder.GetActualModel<TModel>();
138+
139+
return new ModelDetailsTestBuilder<TModel>(actualBuilder.TestContext);
140+
}
141+
92142
/// <summary>
93143
/// Tests whether a deeply equal object to the provided one is returned from the invoked method.
94144
/// </summary>

test/MyTested.AspNetCore.Mvc.ModelState.Test/BuildersTests/ModelsTests/ModelErrorTestBuilderTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ public void AndProvideTheModelShouldReturnProperModelWhenThereIsResponseModelWit
146146
}));
147147
}
148148

149+
[Fact]
150+
public void AndProvideTheModelShouldReturnProperModelWhenThereIsResponseModelWithPredicate()
151+
{
152+
MyController<MvcController>
153+
.Instance()
154+
.Calling(c => c.OkResultWithResponse())
155+
.ShouldReturn()
156+
.Ok(ok => ok
157+
.WithModelOfType<List<ResponseModel>>(m => m.Count == 2)
158+
.ShouldPassForThe<List<ResponseModel>>(responseModel =>
159+
{
160+
Assert.NotNull(responseModel);
161+
Assert.IsAssignableFrom<List<ResponseModel>>(responseModel);
162+
Assert.Equal(2, responseModel.Count);
163+
}));
164+
}
165+
149166
[Fact]
150167
public void AndProvideTheModelShouldReturnProperModelWhenThereIsResponseModelWithModelStateError()
151168
{

test/MyTested.AspNetCore.Mvc.Models.Test/BuildersTests/ModelsTests/ResponseModelDetailsTestBuilderTests.cs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class ResponseModelDetailsTestBuilderTests
1313
{
1414
[Fact]
15-
public void WithResponseModelShouldNotThrowExceptionWithCorrectAssertions()
15+
public void WithResponseModelShouldNotThrowExceptionPassingCorrectAssertions()
1616
{
1717
MyController<MvcController>
1818
.Instance()
@@ -28,7 +28,22 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectAssertions()
2828
}
2929

3030
[Fact]
31-
public void WithResponseModelShouldThrowExceptionWithIncorrectAssertions()
31+
public void WithResponseModelShouldNotThrowExceptionWithActionWithCorrectAssertions()
32+
{
33+
MyController<MvcController>
34+
.Instance()
35+
.Calling(c => c.OkResultWithResponse())
36+
.ShouldReturn()
37+
.Ok(ok => ok
38+
.WithModelOfType<ICollection<ResponseModel>>(m =>
39+
{
40+
Assert.Equal(2, m.Count);
41+
Assert.Equal(1, m.First().IntegerValue);
42+
}));
43+
}
44+
45+
[Fact]
46+
public void WithResponseModelShouldThrowExceptionPassingIncorrectAssertions()
3247
{
3348
Assert.Throws<EqualException>(
3449
() =>
@@ -48,7 +63,26 @@ public void WithResponseModelShouldThrowExceptionWithIncorrectAssertions()
4863
}
4964

5065
[Fact]
51-
public void WithResponseModelShouldNotThrowExceptionWithCorrectPredicate()
66+
public void WithResponseModelShouldThrowExceptionWithActionWithIncorrectAssertions()
67+
{
68+
Assert.Throws<EqualException>(
69+
() =>
70+
{
71+
MyController<MvcController>
72+
.Instance()
73+
.Calling(c => c.OkResultWithResponse())
74+
.ShouldReturn()
75+
.Ok(ok => ok
76+
.WithModelOfType<ICollection<ResponseModel>>(m =>
77+
{
78+
Assert.Equal(1, m.First().IntegerValue);
79+
Assert.Equal(3, m.Count);
80+
}));
81+
});
82+
}
83+
84+
[Fact]
85+
public void WithResponseModelShouldNotThrowExceptionPassingCorrectPredicate()
5286
{
5387
MyController<MvcController>
5488
.Instance()
@@ -60,7 +94,18 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectPredicate()
6094
}
6195

6296
[Fact]
63-
public void WithResponseModelShouldThrowExceptionWithWrongPredicate()
97+
public void WithResponseModelShouldNotThrowExceptionWithCorrectPredicate()
98+
{
99+
MyController<MvcController>
100+
.Instance()
101+
.Calling(c => c.OkResultWithResponse())
102+
.ShouldReturn()
103+
.Ok(ok => ok
104+
.WithModelOfType<ICollection<ResponseModel>>(m => m.First().IntegerValue == 1));
105+
}
106+
107+
[Fact]
108+
public void WithResponseModelShouldThrowExceptionPassingWrongPredicate()
64109
{
65110
Test.AssertException<ResponseModelAssertionException>(
66111
() =>
@@ -75,5 +120,21 @@ public void WithResponseModelShouldThrowExceptionWithWrongPredicate()
75120
},
76121
"When calling OkResultWithResponse action in MvcController expected response model IList<ResponseModel> to pass the given predicate, but it failed.");
77122
}
123+
124+
[Fact]
125+
public void WithResponseModelShouldThrowExceptionWithWrongPredicate()
126+
{
127+
Test.AssertException<ResponseModelAssertionException>(
128+
() =>
129+
{
130+
MyController<MvcController>
131+
.Instance()
132+
.Calling(c => c.OkResultWithResponse())
133+
.ShouldReturn()
134+
.Ok(ok => ok
135+
.WithModelOfType<IList<ResponseModel>>(m => m.First().IntegerValue == 2));
136+
},
137+
"When calling OkResultWithResponse action in MvcController expected response model IList<ResponseModel> to pass the given predicate, but it failed.");
138+
}
78139
}
79140
}

0 commit comments

Comments
 (0)