c# - 如何测试这个业务逻辑

标签 c# asp.net-mvc unit-testing testing

您好,如果有人可以帮助我进行单元测试,我将非常高兴 Visual Studio 单元测试的业务逻辑..

我用谷歌“编辑”并检查了不同的单元测试方法,但我保留了 找到我不需要的 Controller 的单元测试..我会最 很高兴有人可以帮助我了解如何对该方法进行单元测试。

这是我的商务舱

public void AddConsultation(ConsultationView cv, int patientid)
{

    using (var deprepo = new ConsultationRepository())
    {
        if (cv.ConsultId == 0)
        {
            var curr = DateTime.Now;
            string date = curr.ToString("d");
            string  time= curr.ToString("t");
            var patient = da.Patients.ToList().Find(x => x.PatientId == patientid);

            Consultation _consultation = new Consultation
            {
              ConsultId = cv.ConsultId,
              ConsultDate = date,
              ConsultTime = time,
              illness = cv.illness,
              PresribedMed = cv.PresribedMed,
              Symptoms = cv.Symptoms,
              U_Id = patient.PatientId,
            };

            deprepo.Insert(_consultation);
        }
    }
}

这是我的存储库类

public class ConsultationRepository:IConsultationRepository
{
    private DataContext _datacontext = null;
    private readonly IRepository<Consultation> _clinicRepository;

    public ConsultationRepository()
    {
        _datacontext = new DataContext();
        _clinicRepository = new RepositoryService<Consultation>(_datacontext);

    }

    public Consultation GetById(int id)
    {
        return _clinicRepository.GetById(id);
    }

    public List<Consultation> GetAll()
    {
        return _clinicRepository.GetAll().ToList();
    }

    public void Insert(Consultation model)
    {
        _clinicRepository.Insert(model);
    }

    public void Update(Consultation model)
    {
        _clinicRepository.Update(model);
    }

    public void Delete(Consultation model)
    {
        _clinicRepository.Delete(model);
    }

    public IEnumerable<Consultation> Find(Func<Consultation, bool> predicate)
    {
        return _clinicRepository.Find(predicate).ToList();
    }

    public void Dispose()
    {
        _datacontext.Dispose();
        _datacontext = null;
    }
}

最佳答案

您可以让工厂为测试创建不同的存储库

//Interface for a factory class
public interface IFactory
{
    IIConsultationRepository Create();
}

创建两个工厂,一个用于测试,一个用于生产

public class MyFactory : IFactory
{
    public IIConsultationRepository Create()
    {
        return new ConsultationRepository();
    }
}

public class MyTestFactory : IFactory
{
    public IIConsultationRepository Create()
    {
        return new ConsultationTestRpository();
    }
}

创建两个存储库。一个用于测试,一个用于生产

public class ConsultationTestRpository : IConsultationRepository
{
    //Your test repository. In this you skip the database.
    //This is just one simple example of doing it.
    Consultation _consultation;
    public Consultation GetById(int id)
    {
        return _consultation;
    }


    public void Insert(Consultation model)
    {
        _consultation = model;
    }

}

public class ConsultationRepository : IConsultationRepository
{
    //Your repository
}

将其用于生产

var obj = new TheConsultationClass(new MyFactory());

这是为了测试

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var objForTesting = new TheConsultationClass(new MyTestFactory());

        var consultationView = new ConsultationView();



        objForTesting.AddConsultation(consultationView, 123);

        var consultation = objForTesting.GetById(...);

        Assert.AreEqual(123, consultation.U_Id );
    }
}

编辑

我忘了展示如何使用工厂。将其作为参数发送给构造函数,然后调用 Factory.Create()

public class TheConsultationClass
{
    public MyFactory Factory { get; set; }

    public TheConsultationClass(IFactory factory)
    {
        Factory = factory;
    }

    public void AddConsultation(ConsultationView cv, int patientid)
    {

        using (var deprepo = Factory.Create())
        {
            if (cv.ConsultId == 0)
            {
                var curr = DateTime.Now;
                string date = curr.ToString("d");
                string time = curr.ToString("t");
                var patient = da.Patients.ToList().Find(x => x.PatientId == patientid);

                Consultation _consultation = new Consultation
                {
                    ConsultId = cv.ConsultId,
                    ConsultDate = date,
                    ConsultTime = time,
                    illness = cv.illness,
                    PresribedMed = cv.PresribedMed,
                    Symptoms = cv.Symptoms,
                    U_Id = patient.PatientId,
                };

                deprepo.Insert(_consultation);
            }
        }
    }
}

关于c# - 如何测试这个业务逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712516/

相关文章:

c# - .NET Core 基于角色的身份 Cookie 动态过期

asp.net-mvc - 错误 3027 : No mapping specified for the following EntitySet/AssociationSet - sysdiagrams(deleted the sysdiagram table by mistake)

c# - 缓慢嵌套 'for' 循环读取 Excel 对象

c# - 存储有关已登录 ASP.NET MVC 用户的附加信息

c# - IUserStore<TUser>.CreateAsync : how to indicate failure in custom implementation?

unit-testing - 使用 vstest.console 和参数执行测试

php - session 开始/关闭的单元测试问题

javascript - JS : How to mock a nested function in a jestJS unit test?

c# - 将接口(interface) IDL 文件转换为 C#

c# - 如何优化以下 linq to object 查询