Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DeviceClient mockable #110

Closed
avranju opened this issue May 17, 2017 · 4 comments
Closed

Make DeviceClient mockable #110

avranju opened this issue May 17, 2017 · 4 comments
Labels
enhancement New feature or request.

Comments

@avranju
Copy link
Contributor

avranju commented May 17, 2017

Could we please have DeviceClient implement an interface called IDeviceClient so it's mockable for unit testing purposes in downstream projects? Thanks!

@yfakariya
Copy link
Contributor

Personally, I use own wrapper of DeviceClient for mocking in my project. It is great if DeviceClient become mockable!

@tameraw tameraw added the enhancement New feature or request. label May 24, 2017
@CIPop
Copy link
Member

CIPop commented Mar 8, 2018

Regardless of the language, the described problem is a code smell in your application's design. See chapter 8 of Growing Object-Oriented Software, Guided by Tests for reasons to "Only Mock Types That You Own".

Larger SDKs (e.g. .NET Framework) are not providing public interfaces unless those interfaces (a) will never change and (b) must be implemented by application code to achieve certain behaviors (e.g. IDisposable).
Adding such an interface will work only in the version that it is added. Once we add another member to DeviceClient, to respect our binary compatibility promise the IDeviceClient will remain the same and you won't be able to mock new functionality. (We won't consider adding a new interface for each new public API.)

@yfakariya's answer is the correct one: create your own wrapper (facade or adapter). I would also recommend a factory method/class together with the wrapper. That way:

  • You know exactly what functionality your application uses from the type
  • You can adapt in case of undesired changes in behavior
  • You can easily mock

Here are a few online resources:
http://tech.findmypast.com/dont-mock-what-you-dont-own/
https://8thlight.com/blog/eric-smith/2011/10/27/thats-not-yours.html

@StefanSchoof
Copy link

@CIPop it is controversial if "Only Mock Types That You Own" is a Code smell, see Stack Overflow: Should you only mock types you own?

I have have a azure function which is currently written in Node, but since Node Function have no Claims Support (Azure/azure-functions-nodejs-worker#183) I considered, to move this code to c#. But with adding extra wrapper would blow this little function (39 loc and two method calls to the iothub library) disproportionately.

So please reconsider and let the user of the Library decide if they want to mock types they not own.

@cdjc
Copy link

cdjc commented Aug 3, 2020

It seems to me that creating a wrapper for the sole purpose of unit testing is more of a code smell than mocking an object you don't own.

brycewang-microsoft pushed a commit that referenced this issue Jul 14, 2022
Service samples use best practices and dispose
brycewang-microsoft pushed a commit that referenced this issue Jul 15, 2022
Service samples use best practices and dispose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

6 participants