Professional OPC
Development Tools

logos

Online Forums

Technical support is provided through Support Forums below. Anybody can view them; you need to Register/Login to our site (see links in upper right corner) in order to Post questions. You do not have to be a licensed user of our product.

Please read Rules for forum posts before reporting your issue or asking a question. OPC Labs team is actively monitoring the forums, and replies as soon as possible. Various technical information can also be found in our Knowledge Base. For your convenience, we have also assembled a Frequently Asked Questions page.

Do not use the Contact page for technical issues.

Mocking IEasyDAClient

More
28 Oct 2017 07:33 #5615 by support
Replied by support on topic Mocking IEasyDAClient
Dear Sir,
Thank you for your interest in our products.

I am afraid that I have to disappoint you. The design with the extension methods is highly intentional and is not going to change.

I understand the unit testing need, and there is always a bit of issue. But you need to understand how it actually works, and I think you will actually like it. From what you wrote, I have a feeling that you have misunderstood which way the control goes.

All important OPC-related functionality is available through the IEasyDAClient interface. It's implementation that is exposed to you is the EasyDAClient object.

There is just one method in this interface for each primary OPC functionality. For example, the reading is represented by the ReadMultipleItems method. This is the most generic one, which accepts an array of so-called argument objects, and returns an array of result objects.

There are many (really many) extension methods built upon ReadMultipleItems method. Some of them have the same name, and are basically overloads to allow the arguments be passed to the method in different ways. There are then extension methods like ReadItem, which takes this concept further, and provides reading of just one item (as a special case), with many overloads as well.

There are two important things to notice here:

1) The extension methods are defined on the interface, not on its concrete implementation.

2) The extension methods are simple wrappers that perform in-memory manipulations of the arguments and results. The actual OPC operation is performed only through the interface that is implicitly passed to the method. The code around does not do other OPC operations or anything alike.

With this knowledge, your mocking becomes actually quite easier. You just have to mock the principal methods, those on the IEasyDAClient interface, and not all the overloads (I think that their number can go to hundreds). And you have a guarantee that the extension method code is always the same. The unit-testing purist would probably object, but the is highly practical, and works.

There are actually multiple internal implementations of the interface, and the extension methods are used with them too, but this is just to explain the design - it does not affect you.

Zbynek Zahradnik

Please Log in or Create an account to join the conversation.

More
27 Oct 2017 20:23 #5614 by Ian
Mocking IEasyDAClient was created by Ian
Hello.

I just started using the EasyDAClient this week and was very happy to discover that it implements an interface, IEasyDAClient. That means I can mock the interface using my favourite mocking engine for my unit tests.

But my happiness was short-lived as I quickly discovered that the methods that I actually use (ReadItemValue, WriteItemValue, etc) are not declared as part of the interface itself, but are in fact extension methods defined in IEasyDAClientExtension. This unfortunately means that those methods are not mockable and therefore I cannot perform unit testing without having an actual opc server set up.

Are there any plans (read: would it be possible?) to declare these extension method signatures directly in the interface? Even if your concrete implementation simply uses the extension method itself, having the methods defined in the interface would be invaluable to unit testers.

Regards,

Ian

Please Log in or Create an account to join the conversation.

Moderators: support
Time to create page: 0.045 seconds