1

I am currently trying to create a unittest for a override I made, but unittesting it seems a bit more difficult than I expected..

public class Extension : Original
{
    public fallback(HttpContext context)
    {
       return new HttpContext();
    }  

    public IEnumerable<SiteDefinition> RunOn => new[]
    {
       "Some", "Dome", "Gone"
    };

    public bool AllowedOnSite(string site)
    {
        bool allowed = RunOn.Any(sd => sd.Name == site);
        return allowed;
    }

    protected override bool Process(HttpContext context)
    {
        var site = Context.Site.Name;
        if (Context.Domain.Name == "sitecore" || !AllowedOnSite(site))
        {
            return fallback(context);
        } 
     ....

    }

I seem to have a problem with AllowedOnSite, I can unit test it by itself, but not as part og Process, which is needed to unit test the rest of the code?

How do i include AllowedOnSite as always true, as part of the unittest for Process?

Unittest:

    [Theory]
    [CustomAutoData("Gonw")]
    public void AllowedOnSite_IncorrectSite_ReturnFalse(string site)
    {
        //arrange
        var processor = new MediaRequestExtensions();

        //act
        processor.AllowedOnSite(site);

        //assert
        Assert.False(processor.AllowedOnSite(site));
    }

This works as expected, but I don't know how to make this condition true, when I when to unittest the Process Method?

[Theory]
[CustomAutoData("http://gonw.com","Gonw")]
public void Process_RequestURLIsIncorrect_Return(string url, string name)
{
    // Arrange
    var fakeSite = new Sitecore.FakeDb.Sites.FakeSiteContext(new Sitecore.Collections.StringDictionary
    {
        { "name", "Some" },
        { "b", "Dome" },
        { "c", "Gone" }
    });
    HttpRequest httpRequest = new HttpRequest(string.Empty, url, string.Empty);
    HttpResponse httpResponse = new HttpResponse(new StringWriter());
    HttpContext httpContext = new HttpContext(httpRequest, httpResponse);

    var processor = new MediaRequestExtensionsMock
    {
        GetHttpContextFunc = () => httpContext
    };
    using (new Db())
    {
        using (new Sitecore.Sites.SiteContextSwitcher(fakeSite))
        {
            //Sitecore.Context.Site.Name = name;
            // Act
            processor.Process(httpContext);

            // Assert
            Assert.Equal(string.Empty, httpContext.Response.Output.ToString());
        }
    }
}

I trigger the right if statement in in this test, but if I want to trigger to next one, I have to not trigger the first one, which I can't seem to do. I don't see why I have to set fakeSite? it is not being used anyway..

What to do?

1 Answer 1

3

The simplest solution is to change your code to make it more easily unit testable, and remove the dependence on the Sitecore.Context object, and instead pass in the required variables as a parameter to your method.

For example, you could do the following and then unit test the dostuff() method:

public bool process (string url)
{ 
  dostuff(Sitecore.Context.Site.Name);
}

public bool dostuff(string site)
{
  if(site == "something")
  {
    someotherprocess();
  }
}
4
  • But i still have more code, under this if statement which i won't be able to trigger, since i cannot set it?
    – anita
    Jan 3, 2019 at 18:30
  • I don't know what you are trying to do, update your original question with more details.
    – jammykam
    Jan 3, 2019 at 18:58
  • I need to Sitecore.Context.Site a certain value, such that i use that to unittest other condition listed under this first if statement. As you suggest, I could move that out to a seperate function, but that would just result in the same problem, being that I cant set the Sitecore.Context.Site.name..
    – anita
    Jan 3, 2019 at 19:28
  • 1
    Sitecore.Context.Site.Name is a readonly property, you cannot set it. Please update your original question of an example of what you are trying to do.
    – jammykam
    Jan 3, 2019 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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