If I consume a WCF service, the following example client code is generated:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ServiceFault", Namespace="http://schemas.xxx.com/2014/07/Contracts/Faults")]
[System.SerializableAttribute()]
internal partial class ServiceFault : MyProject.Client.WCFService.Fault { ... }
This code is generated with internal
on purpose - it is not meant to be accessed outside the assembly.
If I have, for example, a unit test with the following code (the URL is invalid on purpose, it was not masked for this question):
MyClientWrapper.Connect("http://invalidurl.com/endpoint/service.svc")
It will throw an exception of type System.ServiceModel.FaultException<MyProject.Client.WCFService.ServiceFault>
. The problem I have is that when I define my unit test:
[TestMethod]
[ExpectedException(typeof(FaultException<MyProject.Client.WCFService.ServiceFault>))]
public void ShouldFail() { ... }
The token MyProject.Client.WCFService.ServiceFault
is invalid because it is defined as internal
(again, on purpose).
So how can I expect an exception of a type that is internal to the assembly I'm testing?
Use InternalsVisibleToAttribute. You can modify your AssemblyInfo.cs for example:
[assembly: InternalsVisibleTo("Tests.Project")]