Am besten könnte ich mit Reflektion die versiegelten Klassen instanziieren und Werte in ihren internen Feldern setzen. Hier
ist der Code, den ich kam mit:
private SoapServerMessage CreateSoapServerMessage(
SoapMessageStage stage,
string action,
SoapHeaderCollection headers)
{
var typ = typeof(SoapServerMessage);
// Create an instance:
var constructorInfo = typ.GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, new[] { typeof(SoapServerProtocol) }, null);
var message = (SoapServerMessage)constructorInfo.Invoke(new object[] { CreateSoapServerProtocol(action) });
// Set stage:
var stageField = typ.BaseType.GetField("stage", BindingFlags.NonPublic | BindingFlags.Instance);
stageField.SetValue(message, stage);
// Set headers:
var headersField = typ.BaseType.GetField("headers", BindingFlags.NonPublic | BindingFlags.Instance);
headersField.SetValue(message, headers);
return message;
}
private SoapServerProtocol CreateSoapServerProtocol(string action)
{
var typ = typeof(SoapServerProtocol);
// Create an instance:
var constructorInfo = typ.GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, Type.EmptyTypes, null);
var protocol = (SoapServerProtocol)constructorInfo.Invoke(null);
// Set serverMethod:
var serverMethodField = typ.GetField("serverMethod", BindingFlags.NonPublic | BindingFlags.Instance);
serverMethodField.SetValue(protocol, CreateSoapServerMethod(action));
return protocol;
}
private SoapServerMethod CreateSoapServerMethod(string action)
{
var typ = typeof(SoapServerMethod);
// Create an instance:
var method = new SoapServerMethod();
// Set action:
var actionField = typ.GetField("action", BindingFlags.NonPublic | BindingFlags.Instance);
actionField.SetValue(method, action);
return method;
}
Haben Sie versucht, http://stackoverflow.com/questions/6302177/how-to-unit-test-a-soapextension-derived-class/8330818 # 8330818? –
http://stackoverflow.com/questions/6302177/how-to-unit-test-a-apextension-derived-class – InferOn
Und auch: http://stackoverflow.com/questions/21314672/how-to-mock- soapexception-using-moq-to-unit-test-Fehlerbehandlung # 21349031 – urig