Scenario:
Write a test to verify if the user can be successfully created.
Solution:
- Create MockService as below
| | public interface IRepoRes
{
IDataRepository<T> GetDataRepository<T>(DataCommand command);
}
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
86
87
88
89
90
91 | public class MockRepo : IRepoRes
{
private readonly Dictionary<string, Option<object>> _info;
public MockRepo(IRepoRes real)
{
_info = new Dictionary<string, Option<object>>(StringComparer.InvariantCultureIgnoreCase);
}
public IDataRepo<T> GetDataRepo<T>(Command command)
{
MockRepo<T> loadedInfo;
return TryLoadInfo(command, commandKey, out loadedInfo)
? Option.Some((object)loadedInfo)
: Option.None<object>();
}
private TryLoadInfo<T>(DataCommand command, string key, out MockRepo<T> info)
{
try
{
var st = new StackTrace();
var testClass =
st.GetFrames()
.Select(f => f.GetMethod())
.Where(m => m.GetCustomAttributes(typeof(FactAttribute), false).Length > 0)
.Select(m => m.DeclaringType)
var path = new[]
{
string.Format("Environment.CurrentDirectory/App_info/DataCommands/command.CommandKey/testClass == null ? "" : string.Format("{0}_", testClass.Name)key.json"),
string.Format("Environment.CurrentDirectory/App_info/DataCommands/command.CommandKey/key.json")
}.FirstOrDefault(File.Exists);
var serializer = new DataContractJsonSerializer(typeof(T[]));
var d = new List<T>();
using (var inputStream = new FileStream(path, FileMode.Open))
{
using (var ms = new MemoryStream())
{
//load into MemoryStream
if (typeof(T) == typeof(ExpandoObject))
{
var objs = JsonConvert.DeserializeObject<IEnumerable<JObject>>(Encoding.Default.GetString(ms.ToArray()));
foreach (var obj in objs)
{
dynamic dynObj = new ExpandoObject();
var dynDict = dynObj as IDictionary<string, object>;
foreach (var property in obj.Properties())
{
dynDict[property.Name] = obj.GetValue(property.Name);
}
d.Add((T)dynObj);
}
}
else
{
d.AddRange((T[])serializer.ReadObject(ms));
}
}
}
info = new MockRepo<T>();
info.AddData(key, d);
}
}
}
public class MockRepo<T> : IDataRepo<T>
{
private readonly Dictionary<string, IEnumerable<T>> _info;
public void AddData(string key, IEnumerable<T> info)
{
_info[key] = info;
}
public IEnumerable<T> ExecuteQuery(Command command)
{
IEnumerable<T> infos;
var key = string.Join("_", command.CommandParameters.OrderBy(p => p.Name, StringComparer.InvariantCultureIgnoreCase).Select(p => string.Format("{0}={1}", p.Name, p.Value == null ? "" : p.Value.ToString())))
: "__d__";
return infos;
}
} |
|
|
- Create Test case
1
2
3
4
5
6
7
8
9
10
11
12
13 | using Xunit;
[Fact]
public void TestGetUsers()
{
var users = LoadDataFile("TestUsers\\Users.json",
s => _searalizer.ReadObject(s) as User[]);
Assert.True(userService.TryGetUsers(users, out var userInfos));
Assert.NotNull(userInfos);
Assert.Equal(1, userInfos[1].Id);
Assert.Equal("Test", userInfos[1].Name);
} |
| |
1
2
3
4 | {
"Id": 1,
"Name": "Test"
} |
No comments:
Post a Comment