Try our conversational search powered by Generative AI!

Commerce: ReferenceConverter should be easily mockable


It would be great if the ReferenceConverter could simply get an interface like IReferenceConverter attached to it like most other stuff now has to eliminate the necessity of creating my own facade class for it just to test simple stuff needing it.

May 28, 2014 15:55

Could you provide an example of how the lack of an interface poses a big problem? It should be fairly easy to instantiate and pass to any other service that needs it, in case you can't mock that entire service out of your tests?

Aug 10, 2015 18:19

As this is a while back, I don't remember the exact problem I had, but quickly looking at things now, I suspect that I wanted to inject it via a constructor, and test the service consuming it just by mocking simple methods on the ReferenceConverter.

When testing now, I see that I can in fact just mock the ICatalogSystem it requires - so in practice it's just a matter of reducing the need to inject stuff into the ReferenceConverter to mock it (it's not really super obvious since most people just get stuff intheir constructors). So the following works:

var mock = new Mock<ReferenceConverter>(new Mock<ICatalogSystem>().Object);
mock.Setup(x => x.GetContentLink(It.IsAny<string>())).Returns(new ContentReference(2));

var contentReference = mock.Object.GetContentLink("hey");

However, if I could just say new Mock<ReferenceConverter>() that would make testing it a tiny bit more straigtforward.

Aug 25, 2015 12:56

I think that would work if you did new Mock<ReferenceConverter>(null) which is pretty close.

Aug 25, 2015 13:15

That seems very much to make sense. And so my train of thought might just have been something else in may than it is now - so I suppose this can be regarded as "Ok, won't bother" now :p

Aug 25, 2015 13:16

For future reference:

ReferenceConverter(ICatalogSystem catalogContext) is marked as obsolete in Commerce 10. We have to use Mock<ReferenceConverter>(null, null) instead.

Jun 29, 2017 16:26
Andreas J - Jan 03, 2022 12:46
This should now be the accepted answer.
This thread is locked and should be used for reference only.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.