2

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?

qJake
  • 16,821
  • 17
  • 83
  • 135

2 Answers2

3

Use InternalsVisibleToAttribute. You can modify your AssemblyInfo.cs for example:

[assembly: InternalsVisibleTo("Tests.Project")]
Aleksandr Ivanov
  • 2,778
  • 5
  • 27
  • 35
0

You can add an InternalsVisibleToAttribute to the source assembly:

[assembly: InternalsVisibleTo("TestAssembly")]
Lee
  • 142,018
  • 20
  • 234
  • 287