5

Hello i need to get the url from a Sitecore ítem but without using the LinkManager class from Sitecore because i can't mock it.

What is the best approach to do unit tests when i need information from a Sitecore item?.

Actually i have a SitecoreContext mocked. I need to do an interface to inject LinkManager and mock this interface or exist another option more cleanest.

thank you.

  • Are you using any kind of ORM or Wrapper like Glass Mapper? – Richard Seal Nov 27 '17 at 21:52
4

LinkManager is essentially just a static wrapper around LinkProvider, which uses virtual methods for everything. If you simply inject LinkManager.Provider into your controller's constructor, you can mock the LinkProvider in your tests.

5

If you're on Sitecore 8.2+, you can use abstract BaseLinkManager class (that's actually valid for all the old static managers). In combination with Sitecore Dependency Injection that is easy to inject the abstraction into a controller or a pipeline processor:

public class MyController : ApiController
{
    private readonly BaseLinkManager _linkManager;

    public MyController(BaseLinkManager linkManager)
    {
        _linkManager = linkManager;
    }
}

It allows you to use a mocked version of the manager in your tests and receive a real one in runtime (DefaultLinkManager).

  • 1
    This is arguably a better solution than using the underlying provider, but only works if you are on 8.2+ and regsitering your controllers for DI – Richard Szalay Nov 28 '17 at 19:42
  • 1
    You do not need to register your controllers manually with Sitecore Services Client. All the api controllers are registered OOTB, see the <website>/sitecore/admin/showservicesconfig.aspx page. For versions prior to 8.2 I'd probably create an adapter IMediaManager which one-to-one corresponds to the real static one. It requires just a bit of coding but significantly simplifies the unit testing. – Serhii Shushliapin Nov 28 '17 at 20:02
  • True, but it doesn't apply to rendering controllers, and the default IoC container won't instantiate unregistered classes. – Richard Szalay Nov 28 '17 at 20:44
  • @SergeyShushlyapin Presume you (also) mean BaseLinkManager for "regular" LinkManager for Items – jammykam Nov 28 '17 at 20:47
0

There are a few different ways to handle this, and none of them are perfect (some aren't even free).

I wouldn't avoid the use of LinkManager. You could:

  1. Create a utility class that calls LinkManager, then mock your utility class
  2. Use a tool like Telerik's JustMock which allows you to mock the LinkManager class
  3. Not recommended, but there are ways to allow the Sitecore context to be used during unit testing (I've done this years ago, and wouldn't recommend this route, it's time consuming to setup and there are issues with it, but just noting that it is possible to do, if you wanted to research it).

I love using JustMock myself, but there is a cost to use it. Option 1 is free, but requires a bit of work up front to implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.