-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathModelStateBuilder.cs
More file actions
71 lines (61 loc) · 2.29 KB
/
ModelStateBuilder.cs
File metadata and controls
71 lines (61 loc) · 2.29 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
namespace MyTested.AspNetCore.Mvc.Builders.Models
{
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Routing;
using Contracts.Models;
using Internal.TestContexts;
using Utilities.Extensions;
using System;
/// <summary>
/// Used for building <see cref="ModelStateDictionary"/>
/// </summary>
public class ModelStateBuilder : IAndModelStateBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="ModelStateBuilder"/> class.
/// </summary>
/// <param name="actionContext"><see cref="ModelStateDictionary"/> to build.</param>
public ModelStateBuilder(ActionTestContext actionContext)
=> this.ModelState = actionContext.ModelState;
/// <summary>
/// Gets the <see cref="ModelStateDictionary"/>
/// </summary>
/// <value>The built <see cref="ModelStateDictionary"/></value>
protected ModelStateDictionary ModelState { get; set; }
/// <inheritdoc />
public IAndModelStateBuilder WithError(string key, string errorMessage)
{
this.AddError(key, errorMessage);
return this;
}
/// <inheritdoc />
public IAndModelStateBuilder WithErrors(IDictionary<string, string> errors)
{
errors.ForEach(err => this.AddError(err.Key, err.Value));
return this;
}
/// <inheritdoc />
public IAndModelStateBuilder WithErrors(object errors)
{
var errorsAsDictionary = new RouteValueDictionary(errors);
errorsAsDictionary
.ForEach(err => this.AddError(err.Key, err.Value.ToString()));
return this;
}
/// <inheritdoc />
public IModelStateBuilder AndAlso() => this;
private void AddError(string key, string errorMessage)
=> this.ModelState.AddModelError(key, errorMessage);
/// <inheritdoc />
public IAndModelStateBuilder Passing(Action assertions)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public IAndModelStateBuilder Passing(Func<bool> predicate)
{
throw new NotImplementedException();
}
}
}