Try our conversational search powered by Generative AI!

GetUrl return null value in Unit test

Vote:
 

Hi there!

I have a Unit test, and in the function, I've used GetUrl() but they gave me a null value.

This is the mock config.

GetMock<UrlResolver>()
            .Setup(x => x.GetUrl(It.Is<ContentReference>(y => y == contentReference), It.Is<string>(y => y == "de-CH")))
            .Returns("de-ch/test.html");
GetMock<UrlResolver>()
            .Setup(x => x.GetUrl(It.Is<ContentReference>(y => y == contentReference), It.Is<string>(y => y == "it-CH")))
            .Returns("it-ch/test.html");
GetMock<UrlResolver>()
            .Setup(x => x.GetUrl(It.Is<ContentReference>(y => y == contentReference), It.Is<string>(y => y == "fr-CH")))
            .Returns("fr-ch/test.html");

and in the services class, I'm using DI

public class ServicesClass
    {
        private readonly UrlResolver _urlResolver;

        public HrefLangsViewComponent(
            UrlResolver urlResolver)
        {
            _urlResolver = urlResolver;
        }

In this service, I've used GetUrl(), but it always gives me a null value.

var url = _urlResolver.GetUrl(contentLink, hrefWebsitePrefix);

I don't know why, anybody has the solutions?

Thanks.

#306772
Aug 14, 2023 14:18
Vote:
 

did you debug to see if contentLink matches contentReference, and hrefWebsitePrefix is exactly "de-CH" etc.? because == requires case sensetive 

#306773
Aug 14, 2023 15:41
Roger - Aug 14, 2023 16:32
Oh, exactly. I've set hrefWebsitePrefix to lower in the logic code.
Thank you very much!
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.