首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试此业务逻辑

如何测试此业务逻辑
EN

Stack Overflow用户
提问于 2015-06-08 14:56:57
回答 2查看 986关注 0票数 1

嗨,如果有人能帮我在Visual单元测试上测试这个业务逻辑,我真的很高兴。

我搜索了“ed”并检查了不同的单元测试方法,但是我一直在为控制器找到不需要的单元测试。我很高兴有人能帮助我知道如何对这个方法进行单元测试。

这是我的公务舱

代码语言:javascript
复制
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);
        }
    }
}

这是我的仓库类

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-06-08 15:16:55

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

代码语言:javascript
复制
//Interface for a factory class
public interface IFactory
{
    IIConsultationRepository Create();
}

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

代码语言:javascript
复制
public class MyFactory : IFactory
{
    public IIConsultationRepository Create()
    {
        return new ConsultationRepository();
    }
}

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

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

代码语言:javascript
复制
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
}

用这个来生产

代码语言:javascript
复制
var obj = new TheConsultationClass(new MyFactory());

这是为了测试

代码语言:javascript
复制
[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()

代码语言:javascript
复制
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);
            }
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2015-06-08 22:37:50

如果您阅读并理解了测试控制器的示例,那么测试普通类和方法应该是非常简单的。这基本上是一样的,但您可以更好地控制您的类结构。

就目前情况而言,您的代码存在一些问题,使其难以测试。让我们看看您现有的代码:

代码语言:javascript
复制
public void AddConsultation(ConsultationView cv, int patientid)
{

下面的行创建一个新的ConsultantRepository。这很难模拟,这意味着很难检查存储库是否被正确调用。更好的方法是通过构造函数将存储库或工厂注入到类中。可以像@AxdorphCoder所描述的那样手工滚动,也可以使用IOC容器(如CastleWindsor尼尼特 )为您做一些繁重的工作。

代码语言:javascript
复制
    using (var deprepo = new ConsultationRepository())
    {
        if (cv.ConsultId == 0)
        {

下一行引用DateTime.Now。同样,这也是很难测试的。同样,如果需要知道某个特定日期被使用,最简单的解决方案是为日期注入一个工厂,或者注入该日期。如果不需要如此精确,另一种方法是在测试开始时存储时间,然后验证所使用的时间介于测试开始时间和测试断言建立时间之间。

代码语言:javascript
复制
            var curr = DateTime.Now;
            string date = curr.ToString("d");
            string  time= curr.ToString("t");

下一行引用da。在您给出的代码示例中,这是没有初始化的。好像是另一个仓库..。在这种情况下,上面关于注入存储库的建议就成立了。顺便说一句,如果找不到病人会发生什么?

代码语言:javascript
复制
            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);
        }
    }
}

那么,您将如何测试AddConsultant方法呢?让我们假设您将遵循测试安排的AAA模式。下面是一些您可以编写的测试:

  1. 如果已经设置了ConsultId,则不会更新Validate 安排-安装模拟存储库-安装da.Patients以包含在测试注入存储库中的正确id -创建系统的病人-创建视图,使用ConsultId <> 0 Act sut.AddConsultation(view,somePationId)断言-没有在注入存储库上调用方法。
  2. 验证使用视图中的值创建的协商 安排-创建视图传递,包含consultId =0-安装da.Patients以包含病人与正确的id存储日期时间测试开始创建模拟存储库,并设置它期望调用插入创建系统在测试和注入respository调用sut.AddConsultation(视图,somePationId)断言-断言在重新存储库上被调用了一个咨询断言,每个咨询属性都有预期值,从排列断言咨询日期是>=,安排日期和<=现在。
  3. 验证如果找不到病人,AddConsultation就会失败 安排-创建视图传递,包含consultId =0设置da.Patients,它不包含有正确id创建模拟存储库的病人,并将其设置为不期望测试中调用创建系统和注入Repository call sut.AddConsultation(视图,somePationId)断言-断言异常抛出的正确信息(当前尚未完成)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30712516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档