You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB
C#

7 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Moq;
using Moq.Internals;
using Moq.Language;
using Moq.Protected;
using xUnitStudy.IBll;
using xUnitStudy.Bll;
using xUnitStudy.IDal;
using xUnitStudy.Model;
namespace xUnitStudy.WebApi.Test
{
public class StudentBllTest:IDisposable
{
7 years ago
#region 准备
private StudentBll actualBll;
7 years ago
public StudentBllTest()
{
7 years ago
//这里创建和设置需要注入的IDal。当然也可以创建和设置直接注入。
actualBll = new Bll.StudentBll();
7 years ago
}
7 years ago
#endregion
7 years ago
[Fact]
7 years ago
public void Students_Test()
7 years ago
{
7 years ago
IStudentBll actual_Bll = new StudentBll();
List<Student> studentsFromProperty = actual_Bll.Students;
List<Student> studentsFromMethod = actual_Bll.GetAll();
Assert.Equal(studentsFromMethod, studentsFromProperty);
}
[Fact]
public void GetAll_Test()
{
List<Student> students = actualBll.GetAll();
Assert.Equal(actualBll.Students, students);
}
[Fact]
public void GetStudentById_Test()
{
var studentId = 1;
Student student = actualBll.GetStudentById(studentId);
Assert.NotNull(student);
Assert.Equal(studentId, student.Id);
}
[Fact]
public void AddStudent_Test()
{
var exitsStudent = new Student() { Id=1,Name="lishi",Age=40,lever=0};
var newStudent = new Student() { Id=100,Name="wangwu",Age=30,lever=0};
var exitsResult = actualBll.AddStudent(exitsStudent);
var newResult = actualBll.AddStudent(newStudent);
Assert.False(exitsResult.result);
Assert.True(newResult.result);
Assert.Contains(newStudent, actualBll.GetAll());
}
[Fact]
public void UpdateStudent_Test()
{
var exitsStudent = new Student() { Id = 1, Name = "lishi", Age = 40, lever = 0 };
var newStudent = new Student() { Id = 100, Name = "wangwu", Age = 30, lever = 0 };
var exitsResult = actualBll.UpdateStudent(exitsStudent);
var newResult = actualBll.UpdateStudent(newStudent);
Assert.True(exitsResult.result);
Assert.Contains(exitsStudent, actualBll.GetAll());
Assert.False(newResult.result);
}
[Fact]
public void DeleteStudent_Test()
{
var exitsStudent = new Student() { Id = 1, Name = "lishi", Age = 40, lever = 0 };
var newStudent = new Student() { Id = 100, Name = "wangwu", Age = 30, lever = 0 };
var exitsResult = actualBll.DeleteStudent(exitsStudent.Id);
var newResult = actualBll.DeleteStudent(newStudent.Id);
Assert.True(exitsResult);
Assert.DoesNotContain(exitsStudent, actualBll.GetAll());
Assert.False(newResult);
}
7 years ago
/// <summary>
/// 获取学费
/// 属性注入IDal Mock对象
/// </summary>
[Fact]
public void GetTuition_UseMoq_Test()
{
// #准备
7 years ago
Mock<IStudentDal> mockStudentDal = new Mock<IStudentDal>();
mockStudentDal
.Setup(m => m.GetStudentById(2))
.Returns
(
new Student() { Id = 2, Name = "小小张", Age = 95 }
);
7 years ago
//属性注入,也可以使用构造函数注入
actualBll.dal = mockStudentDal.Object;
// #使用
var student = actualBll.GetStudentById(2);
var tuition = actualBll.GetTuition(2);
7 years ago
7 years ago
// #断言
7 years ago
Assert.Equal(student.Id + student.Age, tuition);
}
7 years ago
#region 清理
7 years ago
public void Dispose()
{
}
7 years ago
#endregion
7 years ago
}
}