Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 84751

Mocking Private Methods and using MemberData Attribute xUnit

$
0
0

I noticed some behavior with maybe xUnit and JustMock when testing private methods.   By below setup works fine and the tests run, however in my "MemberData" class, if i pass in more that one dataset for testing, then when the second time the test runs with the new set of data, the private method code never gets executed.   It gets to the "CreateUpdateFavoriteFacility" code in my api class and then just "steps over" the call to my private method. 

 

 

Here is my Test Setup:

[Theory]
        [MemberData("FacilityFavorite",
            MemberType =typeof(FavoriteTestData))]
        [Trait("BusinessTests", "FacilityTests")]
        publicvoidCreateUpdateFavoriteFacility_CreateUpdateFavorites(Guid employeeID, Guid favoriteFacilityID)
        {
            //arrange
            var context = Mock.Create<jhaCommon>(Constructor.Mocked);
            var api = Mock.Create<JHADirectoryServiceAPI>(context);
 
            Mock.Arrange(() => api.CreateUpdateFavoriteFacility(Arg.AnyGuid, Arg.AnyGuid)).CallOriginal().MustBeCalled();
            Mock.Arrange(() => Logger.LogError(Arg.IsAny<Exception>())).OccursNever();
            Mock.Arrange(() => context.FavoriteTypes.Department()).Returns(() => FakeDataSource.FakeFavoriteTypes().Where(ft => ft.FavoriteType1.ToLower() == "department").FirstOrDefault());
            Mock.Arrange(() => context.PhonelistFavorites).ReturnsCollection(FakeDataSource.FakePhoneListFavorites());
 
            dynamic apiWrap = Mock.NonPublic.Wrap(api);
            Mock.NonPublic.Arrange(apiWrap.CreateUpdateFavorite(ArgExpr.IsAny<Guid>(), ArgExpr.IsAny<Guid>(), ArgExpr.IsAny<FavoriteType>())).CallOriginal();
             
 
            //act
            var client = newJHADirectoryService(api);
            client.CreateUpdateFavoriteFacility(employeeID, favoriteFacilityID);
 
            //assert
            Mock.Assert(api);
        }

 

Here is my TestDataClass

publicstaticclassFavoriteTestData
    {
        #region Private Members
        privatestaticList<object[]> _facilityFavorite = newList<object[]>
        {
            newobject[] { newGuid("00000000-0000-0000-0000-000000000000") , newGuid("00000000-0000-0000-0000-000000000000") },
            newobject[] { newGuid("11111111-1111-1111-1111-111111111111") , newGuid("11111111-1111-1111-1111-111111111111") }
             
        };
        #endregion
 
        #region Public Properties
        publicstaticIEnumerable<object[]> FacilityFavorite
        {
            get{ return_facilityFavorite; }
        }
        #endregion
    }

 

Here is my code in my client class.  This is the entry point of my test:

publicvoidCreateUpdateFavoriteFacility(Guid employeeID, Guid favoriteEmployeeID)
       {
           try
           {
               _api.CreateUpdateFavoriteFacility(employeeID, favoriteEmployeeID);
           }
           catch(Exception ex)
           {
               Logger.LogError(ex);
           }
       }

Here is my "api" Code:

publicvoidCreateUpdateFavoriteFacility(Guid employeeID, Guid favoriteFacilityID)
{
   CreateUpdateFavorite(employeeID, favoriteFacilityID, _context.FavoriteTypes.Department());
}
privatevoidCreateUpdateFavorite(Guid employeeID, Guid favoriteObjectID, FavoriteType type)
{
  //do stuff
}

Viewing all articles
Browse latest Browse all 84751

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>