Skip to content

Commit ef40826

Browse files
committed
Added AndNoModelErrorFor check
1 parent dcd8df2 commit ef40826

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

TestStack.FluentMVCTesting.Tests/ModelErrorTestTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ public void Chain_call_to_check_model_error_in_property()
9696
Assert.That(_modelErrorTest.AndModelErrorFor(m => m.Property1), Is.EqualTo(returnVal));
9797
}
9898

99+
[Test]
100+
public void Chain_call_to_check_no_model_error_in_property()
101+
{
102+
var returnVal = Substitute.For<IModelTest<TestViewModel>>();
103+
_modelTest.AndNoModelErrorFor(Arg.Any<Expression<Func<TestViewModel, string>>>()).Returns(returnVal);
104+
105+
Assert.That(_modelErrorTest.AndNoModelErrorFor(m => m.Property1), Is.EqualTo(returnVal));
106+
}
107+
99108
[Test]
100109
public void Chain_call_to_check_model_error_by_key()
101110
{

TestStack.FluentMVCTesting.Tests/ModelTestTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,28 @@ public void Check_for_model_error_in_property()
5555
}
5656

5757
[Test]
58-
public void Check_for_execpected_lack_of_model_error_in_property()
58+
public void Check_for_unexpected_lack_of_model_error_in_property()
5959
{
6060
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
6161
_modelTest.AndModelErrorFor(m => m.Property1)
6262
);
6363
Assert.That(exception.Message, Is.EqualTo("Expected controller 'ViewTestController' to have a model error for member 'Property1', but none found."));
6464
}
65+
66+
[Test]
67+
public void Check_for_no_model_error_in_property()
68+
{
69+
_modelTest.AndNoModelErrorFor(m => m.Property1);
70+
}
71+
72+
[Test]
73+
public void Check_for_unexpected_model_error_in_property()
74+
{
75+
_controller.ModelState.AddModelError("Property1", "error");
76+
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
77+
_modelTest.AndNoModelErrorFor(m => m.Property1)
78+
);
79+
Assert.That(exception.Message, Is.EqualTo("Expected controller 'ViewTestController' to have no model errors for member 'Property1', but found some."));
80+
}
6581
}
6682
}

TestStack.FluentMvcTesting/ModelErrorTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IModelErrorTest<TModel>
1313
IModelTest<TModel> EndingWith(string endMessage);
1414
IModelTest<TModel> Containing(string containsMessage);
1515
IModelErrorTest<TModel> AndModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithError);
16+
IModelTest<TModel> AndNoModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithNoError);
1617
IModelErrorTest<TModel> AndModelError(string errorKey);
1718
}
1819

@@ -74,5 +75,10 @@ public IModelErrorTest<TModel> AndModelError(string errorKey)
7475
{
7576
return _modelTest.AndModelError(errorKey);
7677
}
78+
79+
public IModelTest<TModel> AndNoModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithNoError)
80+
{
81+
return _modelTest.AndNoModelErrorFor(memberWithNoError);
82+
}
7783
}
7884
}

TestStack.FluentMvcTesting/ModelTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace TestStack.FluentMVCTesting
77
public interface IModelTest<TModel>
88
{
99
IModelErrorTest<TModel> AndModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithError);
10+
IModelTest<TModel> AndNoModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithNoError);
1011
IModelErrorTest<TModel> AndModelError(string errorKey);
1112
void AndNoModelErrors();
1213
}
@@ -28,6 +29,14 @@ public IModelErrorTest<TModel> AndModelErrorFor<TAttribute>(Expression<Func<TMod
2829
return new ModelErrorTest<TModel>(this, member, _controller.ModelState[member].Errors);
2930
}
3031

32+
public IModelTest<TModel> AndNoModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithNoError)
33+
{
34+
var member = ((MemberExpression)memberWithNoError.Body).Member.Name;
35+
if (_controller.ModelState.ContainsKey(member))
36+
throw new ViewResultModelAssertionException(string.Format("Expected controller '{0}' to have no model errors for member '{1}', but found some.", _controller.GetType().Name, member));
37+
return this;
38+
}
39+
3140
public IModelErrorTest<TModel> AndModelError(string errorKey)
3241
{
3342
if (!_controller.ModelState.ContainsKey(errorKey) || _controller.ModelState[errorKey].Errors.Count == 0)

0 commit comments

Comments
 (0)