-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathModelErrorTest.cs
More file actions
85 lines (75 loc) · 3.47 KB
/
ModelErrorTest.cs
File metadata and controls
85 lines (75 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
namespace TestStack.FluentMVCTesting
{
public interface IModelErrorTest<TModel>
{
IModelTest<TModel> ThatEquals(string errorMessage);
IModelTest<TModel> BeginningWith(string beginMessage);
IModelTest<TModel> EndingWith(string endMessage);
IModelTest<TModel> Containing(string containsMessage);
IModelErrorTest<TModel> AndModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithError);
IModelTest<TModel> AndNoModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithNoError);
IModelErrorTest<TModel> AndModelError(string errorKey);
}
public class ModelErrorTest<TModel> : IModelErrorTest<TModel>
{
private readonly IModelTest<TModel> _modelTest;
private readonly string _errorKey;
private readonly List<string> _errors;
public ModelErrorTest(IModelTest<TModel> modelTest, string errorKey, IEnumerable<ModelError> errors)
{
_modelTest = modelTest;
_errorKey = errorKey;
_errors = errors.Select(e => e.ErrorMessage).ToList();
}
public IModelTest<TModel> ThatEquals(string errorMessage)
{
if (!_errors.Any(e => e == errorMessage))
{
throw new ModelErrorAssertionException(string.Format("Expected error message for key '{0}' to be '{1}', but instead found '{2}'.", _errorKey, errorMessage, string.Join(", ", _errors)));
}
return _modelTest;
}
public IModelTest<TModel> BeginningWith(string beginMessage)
{
if (!_errors.Any(e => e.StartsWith(beginMessage)))
{
throw new ModelErrorAssertionException(string.Format("Expected error message for key '{0}' to start with '{1}', but instead found '{2}'.", _errorKey, beginMessage, string.Join(", ", _errors)));
}
return _modelTest;
}
public IModelTest<TModel> EndingWith(string endMessage)
{
if (!_errors.Any(e => e.EndsWith(endMessage)))
{
throw new ModelErrorAssertionException(string.Format("Expected error message for key '{0}' to end with '{1}', but instead found '{2}'.", _errorKey, endMessage, string.Join(", ", _errors)));
}
return _modelTest;
}
public IModelTest<TModel> Containing(string containsMessage)
{
if (!_errors.Any(e => e.Contains(containsMessage)))
{
throw new ModelErrorAssertionException(string.Format("Expected error message for key '{0}' to contain '{1}', but instead found '{2}'.", _errorKey, containsMessage, string.Join(", ", _errors)));
}
return _modelTest;
}
public IModelErrorTest<TModel> AndModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithError)
{
return _modelTest.AndModelErrorFor(memberWithError);
}
public IModelErrorTest<TModel> AndModelError(string errorKey)
{
return _modelTest.AndModelError(errorKey);
}
public IModelTest<TModel> AndNoModelErrorFor<TAttribute>(Expression<Func<TModel, TAttribute>> memberWithNoError)
{
return _modelTest.AndNoModelErrorFor(memberWithNoError);
}
}
}