How to mock ContentItem with Moq?

Hi

Since the ContentItem class doesn't have an interface and doesn't have a parameterless constructor or virtual methods & properties how can I mock it for unit testing my code that uses it?

Thanks

Lee

Tagged:

Best Answer

Answers

  • petrs@kentico.com[email protected] Eindhoven, NLMember, Administrator, Kentico Staff admin
    edited August 2017

    Hi @LeeConlin,
    it's not possible at the moment but we'd like to enable it. We've discussed several approaches with my colleagues and we came to the conclusion that the best (cleanest) solution would be to allow faking HTTP responses.

    Basically, this means that we would allow injecting an instance of HttpClient to the DeliveryClient, replacing the standard one. When constructing the HttpClient you could inject your own implementation of HttpMessageHandler which would effectively enable you to mock any response coming from the DeliveryClient based on the data provided by you (no matter whether the response would be a ContentItem, ContentType, or a strongly-typed item model).

    There are even some libraries which make it easy to mock the HttpClient.

    Would this solution work in your scenario?

  • leelee Member ✭✭

    That may work in the future. Not being able to write tests for my code as I'm developing is a problem for me though as I'll likely not get time later to come back and write them and I use a build server to run tests on each commit ... how long will it be before your proposed solution is implemented?

  • petrs@kentico.com[email protected] Eindhoven, NLMember, Administrator, Kentico Staff admin

    Hi @LeeConlin , it've implemented it here. I'll release a new version of the SDK during the weekend.
    Will let you know once it's out.

  • petrs@kentico.com[email protected] Eindhoven, NLMember, Administrator, Kentico Staff admin

    You can download the nuget now: https://www.nuget.org/packages/KenticoCloud.Delivery/4.7.0

    Release notes will be ready later today..

Sign In or Register to comment.