使用Lambda表达式或Func委托进行C#Moq单元测试 [英] C# Moq Unit Testing with Lambda Expression or Func delegate

查看:178
本文介绍了使用Lambda表达式或Func委托进行C#Moq单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Moq和C#进行单元测试.

I am using Moq with C# for unit testing.

我有以下实现要测试:

var jobsSelectionToMapJobModel = new List<Job>();
var jobsSelectionToMapDataTableModel =
                _enumerableWrapperService.Select(jobsSelectionToMapJobModel,
                    x => _convertJobToJobsModelForDataTableService.Convert(umbracoHelper, x));

_enumerableWrapperService.Select()中的Select方法具有以下签名,如Linq一样:

The Select Method in _enumerableWrapperService.Select() has the following signature, like the Linq one:

Enumerable<TResult> Select<TSource, TResult>(IEnumerable<TSource> source,
            Func<TSource, TResult> selector);

我想测试选择器Func委托的内容是否设置了正确的逻辑. umbracoHelper变量是传递给此方法的外部参数.

I would like to test out that the selector Func delegate’s content has the correct logic set in it. The umbracoHelper variable is an external paramater passed on to this method.

如果您使用It.IsAny<Func<TSource, TResult>>(),则我们不会断言该逻辑很好.另一方面,我当时在考虑使用It.Is<Func<TSource,TResult>(func => XXXXXXXX)之类的东西,但无法弄清楚如何使其工作

If you use the It.IsAny<Func<TSource, TResult>>(), we will not be asserting that the logic is fine. On the other hand, I was thinking of using something like It.Is<Func<TSource,TResult>(func => XXXXXXXX), but cannot figure out how to make it work

请参见以下示例:

单元测试示例1

_listToReturn= new List<JobsModelForDataTable>();
_listOfJobs= new List<Job>();
GetMockFor<IEnumerableWrapperService>()
                .Setup(x => x.Select(_listOfJobs,
                     It.IsAny<Func<Job, JobsModelForDataTable>>()))
                .Returns(_listToReturn);

//上面的问题是,您没有断言正确的逻辑,因为它接受具有相同签名的任何Func

//The problem with the above is that you are not asserting the correct logic as it accepts any Func which has that same signature

单元测试示例2

 _listToReturn= new List<JobsModelForDataTable>();
_listOfJobs= new List<Job>();
GetMockFor<IEnumerableWrapperService>()
                .Setup(x => x.Select(_listOfJobs,
                     It.Is<Func<Job, JobsModelForDataTable>>(f => ......)))
                .Returns(_listToReturn);

//这可能是一个选项,但我不知道如何使它工作

//This might be an option but I cannot figure out how to make it work

或者也许我们需要以另一种方式重新思考我们在做什么?

Or maybe we need to rethink what we are doing in another way?

在此方面的任何帮助,我将感激不尽.

I would appreciate any help on this, on how I can test it out.

预先感谢您的帮助.

推荐答案

要以编程方式检查两个表达式是否做相同的事情,即使不是几乎不可能,也是相当困难的.因此,最好的选择是将表达式移动到常规的命名方法中并测试该方法:

It is rather difficult if not practically impossible to check programmatically whether two expressions do the same thing. So your best bet is to move the expression into a regular, named method and test that method:

var jobsSelectionToMapJobModel = new List<Job>();
var jobsSelectionToMapDataTableModel =
                _enumerableWrapperService.Select(jobsSelectionToMapJobModel, ActualMethodToTest);

[...]

internal Something ActualMethodToTest( Job x ) => _convertJobToJobsModelForDataTableService.Convert( _umbracoHelper, x );

并为ActualMethodToTest编写测试,可能对convertJobToJobsModelForDataTableServiceumbracoHelper使用Moq-mocks.当然,还要测试_enumerableWrapperServiceSelect.

and the write a test for ActualMethodToTest, probably using Moq-mocks for convertJobToJobsModelForDataTableService and umbracoHelper. And, of course, a test for _enumerableWrapperService's Select.

这篇关于使用Lambda表达式或Func委托进行C#Moq单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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