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"
)]
public
void
CreateUpdateFavoriteFacility_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 =
new
JHADirectoryService(api);
client.CreateUpdateFavoriteFacility(employeeID, favoriteFacilityID);
//assert
Mock.Assert(api);
}
Here is my TestDataClass
public
static
class
FavoriteTestData
{
#region Private Members
private
static
List<
object
[]> _facilityFavorite =
new
List<
object
[]>
{
new
object
[] {
new
Guid(
"00000000-0000-0000-0000-000000000000"
) ,
new
Guid(
"00000000-0000-0000-0000-000000000000"
) },
new
object
[] {
new
Guid(
"11111111-1111-1111-1111-111111111111"
) ,
new
Guid(
"11111111-1111-1111-1111-111111111111"
) }
};
#endregion
#region Public Properties
public
static
IEnumerable<
object
[]> FacilityFavorite
{
get
{
return
_facilityFavorite; }
}
#endregion
}
Here is my code in my client class. This is the entry point of my test:
public
void
CreateUpdateFavoriteFacility(Guid employeeID, Guid favoriteEmployeeID)
{
try
{
_api.CreateUpdateFavoriteFacility(employeeID, favoriteEmployeeID);
}
catch
(Exception ex)
{
Logger.LogError(ex);
}
}
Here is my "api" Code:
public
void
CreateUpdateFavoriteFacility(Guid employeeID, Guid favoriteFacilityID)
{
CreateUpdateFavorite(employeeID, favoriteFacilityID, _context.FavoriteTypes.Department());
}
private
void
CreateUpdateFavorite(Guid employeeID, Guid favoriteObjectID, FavoriteType type)
{
//do stuff
}