Pregunta

I've got a windows service that is Opening two WCF services. I want to unit test OnStart() and assert that service1.Open() and service2.Open() is being called. OnStart() looks like this:

protected override void OnStart(string[] args)
{
    // host WCF services            
    _service1.Open();           
    _service2.Open();
}

I'm injecting the services in a constructor overload as such:

public WinService(ServiceHostBase service1, 
                                  ServiceHostBase service2)
{
    _service1 = service1;
    _service2 = service2;
    InitializeComponent();
}

I'm using RhinoMocks to generate a Stub of ServiceHostBase like this:

[TestMethod()]
public void WinServiceOnStartCallsDependenciesAsExpected()
{
    ServiceHostBase service1 = MockRepository.GenerateStub<ServiceHostBase>();
    ServiceHostBase service2 = MockRepository.GenerateStub<ServiceHostBase>();
    WinService target = new WinService(service1, service2);
    WinService_Accessor privateTarget = new WinService_Accessor(new PrivateObject(target));     
    privateTarget.OnStart(null);

When my test calls OnStart(), I get a null reference exception when it calls service1.Open(). I've confirmed that service1 is a mocked object at that point and that it is Open() that is throwing the null. I know that Open() is actually a method on System.ServiceModel.Channels.CommunicationObject and I tried Stubbing or Mocking that as well, but I still get the object ref error. It's not a virtual method, so I would think it's just not being overridden by the mocked version, but when I try to set up an Expectation reportservice.Stub(r => r.Open()), I get a different exception about there not being a default timeout, as if it's running the actual CommunicationObject Open() method instead of something RhinoMocky that is throwing the null ref.

All this to say, I'm just looking for help on how to confirm that Open() is being called on my ServiceHost in a Unit Test. =]

¿Fue útil?

Solución

ServiceHostBase.Open calls CommunicationObject.Open under the hood. Which, as part of its implementation, does a lot of different things - like verifying object's state, creating other objects, calling methods, properties and so forth. And since it's not virtual (your assumption was correct), Rhino will call base class implementation.

To make it work, you'll probably have to mock and stub lot of CommunicationObject dependencies and it still will not be certain whether you'll succeed or not (some types/methods might simply be unmockable by Rhino, think static, sealed or other non-virtual). This is why you should either:

  1. Leave this kind of testing to integration tests; you'll then have .Open tested in end-user environment
  2. Introduce a wrapper around ServiceHostBase and pass it as dependency by interface; this involves extra work (new interface and simple wrapper class), but allows you to do exactly what you wanted

Keep in mind that adding wrapper will only delegate problem further (to the wrapper class' Open method, which essentially will call ServiceHostBase.Open - should you unit test that too?). On the other hand, integration tests might not catch issues as fast as unit tests (assuming you run them less often). Depending on how critical you consider OnOpen to be, which way you chose is more or less a judgement call.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top