Error when trying to test an isolated IQueryable

元气小坏坏 提交于 2019-12-24 10:57:13

问题


Context:

I want to test whether a piece of code that purports to layer some operations on top of an IQueryable<T> actually does so. But when I start with code that is known to work, and try to write a test to exercise it, I get an unintelligible error.

Actual Question:

I would like to understand that error, what it means, and why it is happening.

Not an X-Y questions!!

I'm not interested here in what the best way to achieve my original goal is, or whether this test would have achieved it. I'm asking that question separately (link). In this question, I genuinely want to know "what does this error even mean, and why is it happening for code that I know fundamentally works".


Error Message:

System.ArgumentException : Expression of type '' cannot be used for parameter of type
  'System.Linq.IQueryable`1[ADH.Web.API.Tests.Services.QueryableTest+DemoDomainModel]' of method 
  'System.Linq.IQueryable`1[ADH.Web.API.Tests.Services.QueryableTest+DemoViewModel] 
  Select[DemoDomainModel,DemoViewModel](System.Linq.IQueryable`1[ADH.Web.API.Tests.Services.QueryableTest+DemoDomainModel], 
  System.Linq.Expressions.Expression`1[System.Func`2[ADH.Web.API.Tests.Services.QueryableTest+DemoDomainModel,ADH.Web.API.Tests.Services.QueryableTest+DemoViewModel]])'
at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
at ADH.Web.API.Tests.Services.QueryableTest.DemoService.GetAllDemoViewModels() in C:\Work\ADH\ADH\Actuarial Data Hub\ADH.Web.API.Tests\Services\QueryableTest..cs:line 50
at ADH.Web.API.Tests.Services.QueryableTest.CheckQueryableCompiles() in C:\Work\ADH\ADH\Actuarial Data Hub\ADH.Web.API.Tests\Services\QueryableTest..cs:line 71

Simplification of first line:

System.ArgumentException : Expression of type '' cannot be used for parameter of type
  'IQueryable`1[DemoDomainModel]' of method 
  'IQueryable`1[DemoViewModel] Select[DemoDomainModel,DemoViewModel]
  (IQueryable`1[DemoDomainModel], Expression`1[Func`2[DemoDomainModel,DemoViewModel]])'

Code and Test that causes the bug (Classes + unit Test)

using System.Linq;
using NUnit.Framework;
using FakeItEasy;

namespace ADH.Web.API.Tests.Services
{
    public class QueryableTest
    {
        public class DemoDomainModel
        {
            public int Id { get; set; }

            public string Reference { get; set; }

            public int ChildId { get; set; }
            public DemoChildDomainModel Child { get; set; }
        }

        public class DemoChildDomainModel
        {
            public int Id { get; set; }

            public string ChildReference { get; set; }
        }

        public class DemoViewModel
        {
            public int Id { get; set; }
            public string Reference { get; set; }
            public string ChildReference { get; set; }
        }

        public interface IDemoRepository
        {
            IQueryable<DemoDomainModel> GetAllDemos();
        }

        public class DemoService
        {
            private readonly IDemoRepository _demoRepository;

            public DemoService(IDemoRepository configRepository)
            {
                _demoRepository = configRepository;
            }
            public IQueryable<DemoViewModel> GetAllDemoViewModels()
            {
                var demos = _demoRepository.GetAllDemos();

                return demos.Select(demo => new DemoViewModel()
                {
                    Id = demo.Id,
                    Reference = demo.Reference,
                    ChildReference = demo.Child.ChildReference
                });
            }
        }
        private DemoService _demoService;
        private IDemoRepository _fakeDemoRepository;

        [SetUp]
        public void Setup()
        {
            _fakeDemoRepository = A.Fake<IDemoRepository>();
            _demoService = new DemoService(_fakeDemoRepository);
        }

        [Test]
        public void CheckQueryableCompiles()
        {
            _demoService.GetAllDemoViewModels();
        }
    }
}

来源:https://stackoverflow.com/questions/54376855/error-when-trying-to-test-an-isolated-iqueryable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!