service-referencefakeiteasy

FakeItEasy in C# on a servicereference


I have a servicereference with a method I need to use in a test.

The servicereference class is defined as:

public class MyServiceReference : Clientbase<IMyServiceReference>, IMyServiceReference 
{
   public MyServiceReference()
   {
   }

    ..... methods is then defined
}

From my testmethod I have tried both

private MyServiceReference myServiceReferenceFake = A.Fake<MyServiceReference>();
// And
private MyServiceReference myServiceReference = new MyServiceReference();

For both of these is crashes in the constructor with the message:

System.InvalidOperationException: Could not find default endpoint element that references contract.

All I need is to have a callto definition from a method in that class. How can this be solved?


Solution

  • I've no experience with Clientbase, which I assume to be a System.ServiceModel.ClientBase<TChannel>,but I can make some general comments.

    Since you tried first to fake a MyServiceReference, I'll assume that you're not testing that class, and you want to use it as a collaborator for the system under test. In that case, your best bet is to try faking IMyServiceReference. interfaces are very easy to fake, since they don't bring along any behaviour or baggage like faking a class does.

    If you feel you really need to fake a MyServiceReference, then we have to contend with the fact that FakeItEasy will eventually call MyServiceReference(), which will call ClientBase<IMyServiceReference>(), whose documentation says

    Initializes a new instance of the ClientBase<TChannel> class using the default target endpoint from the application configuration file.

    Based on the error you reported, I assume that the application configuration file is not found or does not include the configuration required to create a MyServiceReference. The fact that you get the same error when you just try to instantiate a MyServiceReference directly strengthens my belief.

    So I think your paths forward are either to try faking IMyServiceReference or to provide the configuration that ClientBase<IMyServiceReference> needs.