Skip to content

Commit 22d1b31

Browse files
committed
Beginnings of #25.
1 parent ed98e0e commit 22d1b31

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

TestStack.FluentMVCTesting.Tests/ViewResultTestTests.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Web.Mvc;
1+
using System.Text.RegularExpressions;
2+
using System.Web.Mvc;
23
using NUnit.Framework;
34

45
namespace TestStack.FluentMVCTesting.Tests
@@ -71,7 +72,26 @@ public void Check_for_invalid_model_using_predicate()
7172
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
7273
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null)
7374
);
74-
Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed."));
75+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected view model {{\"Property1\":\"{0}\",\"Property2\":{1}}} to pass the given condition (m => m.Property1 == null), but it failed.", _model.Property1, _model.Property2)));
76+
}
77+
78+
[Test]
79+
public void Check_for_invalid_model_using_predicate_with_two_conditions()
80+
{
81+
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
82+
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null && m.Property2 == 2)
83+
);
84+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected view model {{\"Property1\":\"{0}\",\"Property2\":{1}}} to pass the given condition (m => m.Property1 == null && m.Property2 == 2), but it failed.", _model.Property1, _model.Property2)));
85+
}
86+
87+
[Test]
88+
public void Check_for_invalid_model_using_predicate_with_closure()
89+
{
90+
int capturedOuterVariable = 2;
91+
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
92+
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null && m.Property2 == capturedOuterVariable)
93+
);
94+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected view model {{\"Property1\":\"{0}\",\"Property2\":{1}}} to pass the given condition (m => m.Property1 == null AndAlso m.Property2 == 2), but it failed.", _model.Property1, capturedOuterVariable)));
7595
}
7696

7797
[Test]

TestStack.FluentMvcTesting/ViewResultTest.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Linq.Expressions;
3+
using System.Text.RegularExpressions;
4+
using System.Web.Helpers;
25
using System.Web.Mvc;
36

47
namespace TestStack.FluentMVCTesting
@@ -37,13 +40,21 @@ public ModelTest<TModel> WithModel<TModel>(TModel expectedModel) where TModel :
3740
return test;
3841
}
3942

40-
public ModelTest<TModel> WithModel<TModel>(Func<TModel, bool> predicate) where TModel : class
43+
public ModelTest<TModel> WithModel<TModel>(Expression<Func<TModel, bool>> predicate) where TModel : class
4144
{
4245
var test = WithModel<TModel>();
4346

4447
var model = _viewResult.Model as TModel;
45-
if (!predicate(model))
46-
throw new ViewResultModelAssertionException("Expected view model to pass the given condition, but it failed.");
48+
49+
var modelLex = Json.Encode(model);
50+
var predicateLex = Regex.Replace(predicate.ToString(), "[()]", "");
51+
var compiledPredicate = predicate.Compile();
52+
53+
if (!compiledPredicate(model))
54+
throw new ViewResultModelAssertionException(string.Format(
55+
"Expected view model {0} to pass the given condition ({1}), but it failed.",
56+
modelLex,
57+
predicateLex));
4758

4859
return test;
4960
}

0 commit comments

Comments
 (0)