question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Endless loop when using Automapper on faked IQueryable<T>

See original GitHub issue

Unsure, whether this is an issue with FakeItEasy or Automapper (or even Castle Proxy).

Assuming an IQueryable<Entity> that is mapped using Automapper into an IQueryable<EntityDto> by means of their expression mapping extension. When the original queryable is mocked using FakeItEasy, the enumeration of the mapped queryable never returns. It hogs a CPU at 100%, so I am assuming some kind of endless loop.

public class FakeItEasyTest
{
    private readonly Mapper _mapper;
    private readonly IQueryable<Entity> _queryable;

    public FakeItEasyTest()
    {
        _mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap<Entity, EntityDto>()));
        _queryable = A.Fake<IQueryable<Entity>>();
    }

    [Fact]
    public void CanEnumerateQueryable()
    {
        Assert.Empty(_queryable.ToArray());
    }

    [Fact]
    public void CanEnumerateDataSourceQueryable()
    {
        IQueryable<EntityDto> mappedQueryable = _queryable.UseAsDataSource(_mapper.ConfigurationProvider).For<EntityDto>();

        // never returns!
        Assert.Empty(mappedQueryable.ToArray());
    }

    [Fact]
    public void CanEnumerateProjectedQueryable()
    {
        // never returns!
        IQueryable<EntityDto> mappedQueryable = _queryable.ProjectTo<EntityDto>(_mapper.ConfigurationProvider);
        Assert.Empty(mappedQueryable.ToArray());
    }
}

repo: https://github.com/marcwittke/FakeItEasyVsAutoMapper

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
marcwittkecommented, Nov 10, 2021

Works like a charm, @blairconrad . Thanks, I updated the repro-repo that passes all tests now, even when there is an arbitrary interface faked that has a method returning IQueryable.

I made an attempt with a dummy factory before, but obviously I was missing something. Well, it got pretty late yesterday…

Thanks!

1reaction
blairconradcommented, Nov 10, 2021

@marcwittke, I’m sure you’ll get your configuration working, but if you’re interested, I stole some minutes to implement a DummyFactory that you might enjoy. It seemed to do the trick for me.

public class DummyQueryableFactory : IDummyFactory
{
    public bool CanCreate(Type type)
    {
        if (type.IsGenericType)
        {
            var queryableContentType = type.GetGenericArguments()[0];
            var queryableTypeDefinition = typeof(IQueryable<>).MakeGenericType(queryableContentType);
            return queryableTypeDefinition.IsAssignableFrom(type);
        }
        return false;
    }

    public object Create(Type type)
    {
        var queryableContentType = type.GetGenericArguments()[0];
        var listType = typeof(List<>).MakeGenericType(queryableContentType);
        var list = (IEnumerable)Activator.CreateInstance(listType);
        return Queryable.AsQueryable(list);
    }

    public Priority Priority => Priority.Default;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - AutoMapper throwing StackOverflowException when ...
The problem is an infinite loop when calling mapper on lists. Your mappings are right. You can try to empty lists before convert...
Read more >
IQueryable Extensions Exception · Issue #790
The problem is commented out properties will cause an infinite loop when trying to Project. Project makes a select statement, and DON'T use...
Read more >
The reasons behind why I don't use AutoMapper.
I've encountered cases when AutoMapper transforms simple thing like mapping values from object to other into a really complex problem, which ...
Read more >
4 Common Mistakes with the Repository Pattern
Repositories that return IQueryable. One of the reasons we use the repository pattern is to encapsulate fat queries. These queries make it hard ......
Read more >
IQueryable no supported?
Hello, Isn't possible to map an IQueriable with AutoMapper? Articles = Mapper.Map<IQueryable<Article>, IQueryable<ArticleItemViewModel>>(articleRepository.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found