mercredi 29 juin 2016

Mocking with Substitute.ForPartsOf still goes inside that method

In my UserValidation class I am testing a method which that method is internally calling another method. But I just want to return some fake value for that internal method and move on! But the problem is that it is still actually going inside that method.

The method I want to just give me a mock result is this:

public virtual List<Web_Pharmacy_Users> QueryUsersByEmail(string emailAddress)
{
    var query = (from u in _cactusDbContext.Web_Pharmacy_Users
        where
            (u.Email.Trim().ToUpper() == emailAddress.Trim().ToUpper()
             && u.Active)
        select u);

    return query.ToList();
}

where _cactusDbContext is coming through dependency injection and Web_Pharmacy_Users is a DbSet table.

The way I tried to mock its result is like this below:

    ICactusDbContext fakeDbContext = NSubstitute.Substitute.For<ICactusDbContext>();

    var partiallyFaked = Substitute.ForPartsOf<UserValidation>(fakeDbContext);

    List<Web_Pharmacy_Users> webUsers = new List<Web_Pharmacy_Users>();

    try
    {
        partiallyFaked.QueryUsersByEmail("a@abc.com").Returns(webUsers);
    }
    catch (Exception e)
    {
        var dfgf = e.ToString();
        throw;
    }

But it throws a null exception because it tries to actually run that method so goes to this part of code in _cactusDbContext.Web_Pharmacy_Users and it is null.

I thought it shouldn't even try to go inside and run the actual code. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire