找到不能运行测试原因:并重写测试

master
bicijinlian 7 years ago
parent 3a3e34723e
commit b9610f4fd9

@ -26,6 +26,7 @@ namespace RedisStudyTest
private List<Student> students; private List<Student> students;
private Student student = null; private Student student = null;
private string preHashKey = "RedisStudy:Student:"; private string preHashKey = "RedisStudy:Student:";
private int keyExpireSeconds = 20;
/// <summary> /// <summary>
/// 构造 /// 构造
@ -77,6 +78,10 @@ namespace RedisStudyTest
#endregion #endregion
#region 添加或更新学习 #region 添加或更新学习
/// <summary>
/// 参数异常测试
/// </summary>
[Fact] [Fact]
public void AddStudentExceptionTest() public void AddStudentExceptionTest()
{ {
@ -88,6 +93,9 @@ namespace RedisStudyTest
Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { })); Assert.Throws<ArgumentNullException>(() => hashStudy.HashSet(preHashKey + "-1", new HashEntry[] { }));
} }
/// <summary>
/// 参数When测试
/// </summary>
[Fact] [Fact]
public void AddStudentWhenTest() public void AddStudentWhenTest()
{ {
@ -108,6 +116,66 @@ namespace RedisStudyTest
Assert.True(id_When_Always_NotExists); Assert.True(id_When_Always_NotExists);
} }
/// <summary>
/// 添加一个默认学生
/// </summary>
[Fact]
public void AddStudentTest()
{
string redisKey = preHashKey + student.Id;
var studentEntries = new HashEntry[]
{
new HashEntry("Id",1),
new HashEntry("Name",student.Name),
new HashEntry("Age",student.Age),
};
//插入Sudent
var addHash = hashStudy.HashSet(redisKey, studentEntries, CommandFlags.None);
Assert.True(addHash);
//设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds));
}
/// <summary>
/// 添加一组初始化设置的学生
/// </summary>
[Fact]
public void AddStudentsTest()
{
foreach (var temp in students)
{
string redisKey = preHashKey + temp.Id;
var studentEntries = new HashEntry[]
{
new HashEntry("Id", temp.Id),
new HashEntry("Name", temp.Name),
new HashEntry("Age", temp.Age),
};
//插入Sudent
var addStudent = hashStudy.HashSet(redisKey, studentEntries);
Assert.True(addStudent);
//设置过期
redisDatabase.KeyExpire(redisKey, TimeSpan.FromSeconds(keyExpireSeconds));
}
}
/// <summary>
/// 更新学生异常测试
/// </summary>
[Fact]
public void UpdateStudentExceptionTest()
{
string redisKey = preHashKey + student.Id;
//不存在Key, 返回false,不异常
Assert.False(hashStudy.HashSet(string.Empty, "Id", -1));
}
#endregion #endregion
[Fact] [Fact]

@ -144,7 +144,7 @@ namespace RedisStudyTest
} }
/// <summary> /// <summary>
/// 更新学生 /// 更新学生:导致测试不能运行
/// </summary> /// </summary>
[Theory] [Theory]
[InlineData("Id", 1)] [InlineData("Id", 1)]

@ -141,7 +141,8 @@ namespace RedisStuy
/// </summary> /// </summary>
public bool HashSet(RedisKey key, RedisValue hashField, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None) public bool HashSet(RedisKey key, RedisValue hashField, RedisValue value, When when = When.Always, CommandFlags flags = CommandFlags.None)
{ {
return redisDatabase.HashSet(key, hashField, value, when, flags); var result = redisDatabase.HashSet(key, hashField, value, when, flags);
return result;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save