Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error unitary testing #1

Open
Pakoke opened this issue Sep 4, 2017 · 0 comments
Open

Error unitary testing #1

Pakoke opened this issue Sep 4, 2017 · 0 comments

Comments

@Pakoke
Copy link

Pakoke commented Sep 4, 2017

Hi

I'm implementing a repository.When I try to test it unitary, I receive the next error.

System.NotImplementedException: 'The member 'IEnumerable.GetEnumerator' has not been implemented on type 'FakeSet1' which inherits from 'DbSet1'. Test doubles for 'DbSet`1' must provide implementations of methods and properties that are used.'

Did you ever have this type of error, when you try to get the entities?

I've using several classes from the base test and double test.

My code is:

[TestMethod]
public void GetAllCompetitions_Test()
{
IInterceptorsResolver resolver = GetEmptyInterceptors();
IDbContextFactory factory = GetFactory();

        IRepository repository = GetTarget(factory, resolver);

        IEnumerable<CompetitionDto> actual = repository.GetEntities<CompetitionDto>().ToList();

        CompetitionDto expected = new CompetitionDto { Id = 424 };
        Assert.AreEqual(expected, actual.FirstOrDefault());
        Assert.AreEqual(1, actual.Count());

    }

    private static IInterceptorsResolver GetEmptyInterceptors()
    {
        Mock<IInterceptorsResolver> resolver = new Mock<IInterceptorsResolver>();
        resolver.Setup(x => x.GetEntityInterceptors(It.IsAny<Type>()))
                .Returns(new IEntityInterceptor[0]);
        resolver.Setup(x => x.GetGlobalInterceptors())
                .Returns(new IEntityInterceptor[0]);
        return resolver.Object;
    }

    private static IDbContextFactory GetFactory()
    {
        DbContextFakeWrapper wrapper = new DbContextFakeWrapper();

        List<CompetitionDto> competitions = new List<CompetitionDto>()
        {
            new CompetitionDto()
            {
                Id = 424,
                Caption = "European Championships France 2016",
                Short_Caption = "European Championships France",
                League = "EC",
                Year = 2016,
                NumberOfTeams = 24,
                NumberOfGames = 38,
                LastUpdate = DateTime.UtcNow
            }
        };

        DbSet<CompetitionDto> userSet = competitions.MockDbSet(wrapper);
        wrapper.ContextDouble.Setup(x => x.Set<CompetitionDto>()).Returns(() => userSet);
        
        DbContextFakeWrapper fakeWrapper = wrapper;
        return fakeWrapper.ContextDouble.BuildFactoryStub();
    }

    private static IInterceptorsResolver GetResolver(Expression<Func<IInterceptorsResolver, IEnumerable<IEntityInterceptor>>> getInterceptorsFunction,
        InterceptorDouble interceptorMock)
    {
        Mock<IInterceptorsResolver> stub = new Mock<IInterceptorsResolver>();
        stub.Setup(getInterceptorsFunction).Returns(new[] { interceptorMock });
        return stub.Object;
    }

    protected IRepository GetTarget(IDbContextFactory factory, IInterceptorsResolver resolver)
    {
        return new Repository(factory, resolver);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant