I created a fonction that verify some rules in fact the fonction is supposed to return a bool whenever element of a list match element of another list
Here is the full code of the Rule Class
public override TestResult Execute()
{
Instrument ins = (Items.Length > 0) ? Items[0] as Instrument : null;
string errorInfo;
if (ins == null)
{
Result.Message = "Unable to perform test";
Result.Status = ResultStatus.Error;
return Result;
}
if (MPICSupportDB(ins))
{
Result.Message = "DB not supported by MPIC";
Result.Status = ResultStatus.Yellow;
}
else
{
Result.Status = ResultStatus.Green;
}
return Result;
}
private bool MPICSupportDB(Instrument ins)
{
IServiceProviderFactory serviceFactory = new WebServiceProviderFactory();
IInterfaceAssignmentService wService = serviceFactory.CreateInterfaceAssignmentService();
InterfaceAssignment wAssignments = wService.LoadAssignmentGroup("R4");
return ins.Connections.OfType<InterfaceConnection>().Where(conn => conn.Card.IsDB).Any(conn => wAssignments.PartMasters.Any(partNumber => (conn.CardPartNumber == partNumber.PartNumber)));
//foreach (InterfaceConnection conn in ins.Connections.OfType<InterfaceConnection>())
// if (conn.Card.IsDB)
// {
// for (int i = 0; i < wAssignments.PartMasters.Count; i++)
// foreach (var partNumber in wAssignments.PartMasters)
// if ((conn.CardPartNumber == partNumber.PartNumber))
// return true;
// }
//return false;
}
}
What I'm exactly looking for is to test this fonction called MPICSupportDB in a unit test
private bool MPICSupportDB(Instrument ins)
{
IServiceProviderFactory serviceFactory = new WebServiceProviderFactory();
IInterfaceAssignmentService wService = serviceFactory.CreateInterfaceAssignmentService();
InterfaceAssignment wAssignments = wService.LoadAssignmentGroup("R4");
return ins.Connections.OfType<InterfaceConnection>().Where(conn => conn.Card.IsDB).Any(conn => wAssignments.PartMasters.Any(partNumber => (conn.CardPartNumber == partNumber.PartNumber))); }
So I created this in my test unit but I'm lost and I have no idea what to do
[TestMethod]
public void TestForcompatibleDB()
{
var ins = new Instrument();
var serviceFactoryMock = new Mock<IServiceProviderFactory>();
var wserviceTest = new Mock<IInterfaceAssignmentService>();
var wassagnementTest = new Mock<InterfaceAssignment>();
// adding an MPIC card
ins.Connections.Add(AddCard(CardType.MPIC, "MA505400612268", "CARD1", 0, ins));
// adding an MPIC daughterboard
ins.Connections.Add(AddCard(CardType.GPIM_DB, "MA335022012268", "DB1", 1, ins));
var rule = new Rule026(RuleApplicability.Test, new object[] { ins });
var result = rule.Execute();
Assert.IsNotNull(result);
Assert.AreEqual(ResultStatus.Green, result.Status);
}
the problem is that object like Webservice and factoryService object cannot be run directly in a test unit
Can someone help me in how to properly mock these object and make my test run?
Aucun commentaire:
Enregistrer un commentaire