如何单元测试具有Task.Run(()的Controller方法 [英] how to unit test Controller method which has Task.Run(()

查看:127
本文介绍了如何单元测试具有Task.Run(()的Controller方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Team,

当控制器方法具有Task.Run()

Need to know how to write Nunit test case when the controller method has Task.Run()

时,需要知道如何编写Nunit测试用例例如,请查看以下代码

For example look the below code

 公共类DataProcessController:控制器

    {       



       公共ActionResult索引(DataProcess模型)

        {

           试试
            {

               任务任务= Task.Run(()=>

         {

    ;       尽量

           {

             Database.Insert(模型);

           }

           catch(Exception ex)

           {

        &NBSP ;    Database.Log(ex);

          }

       });

            }¥b $ b            catch(例外情况)

            {

                ModelState.AddModelError("无法处理数据",string.Empty);

               返回此。查看(型号);

            }¥b $ b            return RedirectToAction(" Index");



        }¥b $ b    }

 public class DataProcessController : Controller
    {        

        public ActionResult Index(DataProcess model)
        {
            try
            {
                Task task = Task.Run(() =>
                {
                    try
                    {
                        Database.Insert(model);
                    }
                    catch(Exception ex)
                    {
                        Database.Log(ex);
                    }
             });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("Not able to process the data",string.Empty);
                return this.View(model);
            }
            return RedirectToAction("Index");

        }
    }

************************************** ***********

*************************************************

 公共类DataProcessControllerTest

    {

        [测试]

        public void Index()

        {

            DataProcessController objController = new DataProcessController();

            DataProcess process = new DataProcess(){ProcessId = 501,ProcessName =" MorningBatch" };
            var result = objController.Index(process);

            Assert.IsNotNull(结果);

        }¥b $ b    }

 public class DataProcessControllerTest
    {
        [Test]
        public void Index()
        {
            DataProcessController objController = new DataProcessController();
            DataProcess process = new DataProcess() { ProcessId = 501, ProcessName = "MorningBatch" };
            var result = objController.Index(process);
            Assert.IsNotNull(result);
        }
    }

***********************************

***********************************

当我们运行上面的测试方法时,它没有命中Task中的Database.Insert(model)行。我们需要测试是否插入了记录...

When we run the above test method it is not hitting the line Database.Insert(model) inside the Task. We need to test whether the record got inserted or not...

请帮助如何编写测试用例,它应该触及Controller方法中的所有代码行。

Please help how to write test case and it should hit the all the lines of code in the Controller method.

请帮忙。




推荐答案

朋友,

欢迎来到MSDN论坛。

Welcome to MSDN forum.

>>当我们运行上面的测试方法时,它没有命中Task中的Database.Insert(model)行。我们需要测试是否插入了记录...

>>请帮助如何编写测试用例,它应该打到全部Controller方法中的代码行

根据您的描述,您想知道数据是否已成功插入数据库,这是  集成测试

Based on your description, you want to know whether the data was successfully inserted into the database, this is the integration test.

据我所知,单元测试只是检查代码是否按预期工作。换句话说,单元测试只能检查是否成功调用了
Database.Insert()方法。

As I know, unit test just check the code if it works as expected. In other words, unit tests can only check whether the Database.Insert() method is successfully called or not.

插入方法是异步执行的,因此,当索引方法完成(返回)时,插入的状态是不确定的(可能尚未执行,可能已执行,或者可能已完成) )。

Insert method is executed asynchronously, so, when the index method completes (returns), the state of the insert is indeterminate (perhaps not yet executed, may be executed, or may have been completed).

因此,如果您想进行集成测试,请推迟检查数据是否已插入数据库。或者,如果您想使用单元测试,那么
应该模拟数据库。插入方法并在Dummy方法中进行断言。

Therefore, if you want to do integration test, please defer to check if the data has been insert the DB. Or, if you want to use unit test, you should mock Database. Insert method and make assertions in the Dummy method.

参考:

http://www.artima.com/weblogs/viewpost.jsp?thread=126923

希望有所帮助。

 

祝你好运,

Fletcher


这篇关于如何单元测试具有Task.Run(()的Controller方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆