chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi folks,

I have some code that I'm having a hard time unit testing. Here's the code

var actionLogClass = _entityTypes.GetClass(General.IncidentAnalystCommentLogClass, WorkItemLibraryManagmentPack);
var cemo = new CreatableEnterpriseManagementObject(_managementGroup, actionLogClass);


The actionLogClass is of type ManagementPackClass, which inherits from the abstract class ManagementPackType. This constructor for CreatableEnterpriseManagementObject takes as a second argument the ManagementPackClass concrete type.

I have the following TypeMock code in my unit test:

var fakeMpClass = Isolate.Fake.Instance<ManagementPackClass>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => _fakeEntityTypes.GetClass(General.IncidentAnalystCommentLogClass, fakeManagementPack)).WillReturn(fakeMpClass);


The trouble is that the fakeMpClass is not of type MockManagementPackClass, it's of type MockManagementPackType, leading to the following error on the constructor code:

System.InvalidCastException: Unable to cast object of type 'Mock0009ManagementPackType' to type 'Microsoft.EnterpriseManagement.Configuration.ManagementPackClass'.


How do I go about making sure that my Isolate.Fake<> returns a mock of the concrete class and not the abstract class?

Thanks in advance!
asked by lant3rn1969 (18.4k points)

3 Answers

0 votes
Hi,
I could not reproduce the error you got.
Is possible to post here the full test code?
answered by ohad (35.5k points)
0 votes
Hi,

Have you managed to solve this issue?
If not, please send us the test code.
answered by alex (18k points)
0 votes
Here's the entirety of the test:

//fake global objects
_fakeManagementGroup = Isolate.Fake.Instance<EnterpriseManagementGroup>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => _fakeManagementGroup.IsConnected).WillReturn(true);
_fakeEntityObjects = Isolate.Fake.Instance<IEntityObjectsManagement>(Members.ReturnRecursiveFakes);
_fakeEntityTypes = Isolate.Fake.Instance<IEntityTypeManagement>(Members.ReturnRecursiveFakes);
_fakeManagementPackMgmt = Isolate.Fake.Instance<IManagementPackManagement>(Members.ReturnRecursiveFakes);
_fakeLogger = Isolate.Fake.Instance<ILogger>();
_fakeSettings = Isolate.Fake.Instance<SettingsManager>();

//fake local objects
var incidentId = Guid.NewGuid();
var logGuid = Guid.NewGuid();
var fakeIncident = Isolate.Fake.Instance<CsmIncident>(Members.ReturnRecursiveFakes);
fakeIncident.ServiceManagerObject.ObjectGuid = incidentId;
//var actionLog = Isolate.Fake.Instance<ActionLog>();
//actionLog.Comments = "this is a comment";
var fakeManagementPack = Isolate.Fake.Instance<ManagementPack>(Members.ReturnRecursiveFakes);
var fakeTransaction = Isolate.Fake.AllInstances<IncrementalDiscoveryData>(Members.ReturnRecursiveFakes);
var fakeIncidentEmo = Isolate.Fake.Instance<EnterpriseManagementObject>(Members.ReturnRecursiveFakes);
var fakeMpClass = Isolate.Fake.AllInstances<ManagementPackClass>(Members.ReturnRecursiveFakes);
var fakeRelationship = Isolate.Fake.Instance<ManagementPackRelationship>(Members.ReturnRecursiveFakes);
var fakeLogEmoRel = Isolate.Fake.Instance<EnterpriseManagementRelationshipObject<EnterpriseManagementObject>>(Members.ReturnRecursiveFakes);
var fakeLogEmo = Isolate.Fake.Instance<EnterpriseManagementObject>(Members.ReturnRecursiveFakes);
var fakeCemo = Isolate.Fake.Instance<CreatableEnterpriseManagementObject>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
var fakeCemoRel = Isolate.Fake.Instance<CreatableEnterpriseManagementRelationshipObject>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);

//fake methods and their return values
Isolate.WhenCalled(() => _fakeManagementPackMgmt.GetManagementPacks())
        .WillReturn(new List<ManagementPack> { fakeManagementPack });
var smObjectGuid = fakeIncident.ServiceManagerObject.ObjectGuid;
Isolate.WhenCalled(() => _fakeEntityObjects.GetObject<EnterpriseManagementObject>
    (smObjectGuid, ObjectQueryOptions.Default)).WillReturn(fakeIncidentEmo);
Isolate.WhenCalled(() => _fakeEntityTypes.GetClass(General.IncidentAnalystCommentLogClass, fakeManagementPack)).WillReturn(fakeMpClass);
//Isolate.WhenCalled(() => fakeIncident.ActionLogs).WillReturnCollectionValuesOf(new List<ActionLog> { actionLog });
Isolate.WhenCalled(() => _fakeEntityTypes.GetRelationshipClass
    (General.WorkItemContainsAnalystCommentRelationshipName, fakeManagementPack)).WillReturn(fakeRelationship);
Isolate.WhenCalled(() => _fakeEntityObjects.GetRelationshipObjectsWhereSource<EnterpriseManagementObject>
    (smObjectGuid, fakeRelationship, DerivedClassTraversalDepth.Recursive, TraversalDepth.OneLevel, ObjectQueryOptions.Default))
    .WillReturnCollectionValuesOf(new List<EnterpriseManagementRelationshipObject<EnterpriseManagementObject>> { fakeLogEmoRel });
Isolate.WhenCalled(() => fakeLogEmo.GetClasses()).WillReturnCollectionValuesOf(new List<ManagementPackClass> {fakeMpClass});
Isolate.WhenCalled(() => fakeLogEmoRel.TargetObject).WillReturn(fakeLogEmo);
Isolate.WhenCalled(() => fakeMpClass.GetManagementPack()).WillReturn(fakeManagementPack);
Isolate.WhenCalled(() => fakeLogEmo.Id).WillReturn(logGuid);

//system under test
var scsmDataAccess = new ScsmDataAccess(_fakeManagementGroup, _fakeEntityObjects, 
    _fakeEntityTypes, _fakeManagementPackMgmt, _fakeLogger, _fakeSettings);


//act
var incident = scsmDataAccess.SaveOneIncidentLog(fakeIncident, "currentUser");

//assert
Isolate.Verify.WasCalledWithAnyArguments(() => fakeTransaction.Overwrite(_fakeManagementGroup));
Isolate.Verify.WasCalledWithAnyArguments(fakeCemoRel.Commit);
Assert.AreSame(fakeCemo, fakeCemoRel.TargetObject);
Assert.AreSame(fakeIncidentEmo, fakeCemoRel.SourceObject);
Assert.AreEqual(incidentId, incident.ServiceManagerObject.ObjectGuid);
Assert.AreEqual(1, incident.ActionLogs.Count);
answered by lant3rn1969 (18.4k points)
...