Skip to content

Commit a5a473f

Browse files
committed
Hooked up the ExpressionInspector.
1 parent 587c6f7 commit a5a473f

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

TestStack.FluentMVCTesting.Tests/ViewResultTestTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,26 @@ public void Check_for_invalid_model_using_predicate()
7272
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
7373
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null)
7474
);
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)));
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)));
7676
}
7777

7878
[Test]
79-
public void Check_for_invalid_model_using_predicate_with_two_conditions()
79+
public void Check_for_invalid_model_using_predicate_with_conditional_or()
8080
{
8181
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
82-
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null && m.Property2 == 2)
82+
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null || m.Property2 == 1)
8383
);
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)));
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 == 1))), but it failed.", _model.Property1, _model.Property2)));
8585
}
8686

8787
[Test]
88-
public void Check_for_invalid_model_using_predicate_with_closure()
88+
public void Check_for_invalid_model_using_predicate_with_primitive_operand()
8989
{
90-
int capturedOuterVariable = 2;
90+
_viewResult.ViewData.Model = "abc";
9191
var exception = Assert.Throws<ViewResultModelAssertionException>(() =>
92-
_viewResultTest.WithModel<TestViewModel>(m => m.Property1 == null && m.Property2 == capturedOuterVariable)
92+
_viewResultTest.WithModel<string>(m => m == "ab")
9393
);
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)));
94+
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected view model \"{0}\" to pass the given condition (m => (m == \"ab\")), but it failed.", _viewResult.ViewData.Model)));
9595
}
9696

9797
[Test]
@@ -114,7 +114,7 @@ public void Run_any_assertions_on_the_model()
114114
}
115115
}
116116

117-
class InvalidViewModel {}
117+
class InvalidViewModel { }
118118
public class TestViewModel
119119
{
120120
public string Property1 { get; set; }

TestStack.FluentMvcTesting/ViewResultTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text.RegularExpressions;
44
using System.Web.Helpers;
55
using System.Web.Mvc;
6+
using TestStack.FluentMVCTesting.Internal;
67

78
namespace TestStack.FluentMVCTesting
89
{
@@ -46,8 +47,9 @@ public ModelTest<TModel> WithModel<TModel>(Expression<Func<TModel, bool>> predic
4647

4748
var model = _viewResult.Model as TModel;
4849

50+
var inspector = new ExpressionInspector();
4951
var modelLex = Json.Encode(model);
50-
var predicateLex = Regex.Replace(predicate.ToString(), "[()]", "");
52+
var predicateLex = inspector.Inspect(predicate);
5153
var compiledPredicate = predicate.Compile();
5254

5355
if (!compiledPredicate(model))

0 commit comments

Comments
 (0)