Skip to main content
added 10 characters in body
Source Link

I'd say FakeDb is outdated and I'd not recommend using it nowadays. Sitecore 9 brought abstractions so regular mocks can be utilized.

AutoFixture and Sitecore Customizations for AutoFixture help to reduce the amount of code needed to get started. See how the success scenario could look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(
    Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

I'd say FakeDb is outdated and I'd not recommend using it nowadays. Sitecore 9 brought abstractions so regular mocks can be utilized.

AutoFixture and Sitecore Customizations for AutoFixture help to reduce the amount of code needed to get started. See how the success scenario could look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

I'd say FakeDb is outdated and I'd not recommend using it nowadays. Sitecore 9 brought abstractions so regular mocks can be utilized.

AutoFixture and Sitecore Customizations for AutoFixture help to reduce the amount of code needed to get started. See how the success scenario could look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(
    Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

deleted 28 characters in body
Source Link

I'd say FakeDb is outdated and I'd not recommend using it nowadays. At least, after Sitecore 9 wherebrought abstractions were introduced, there is no need in it at allso regular mocks can be utilized.

An alternative approach is to use mocks, AutoFixture and Sitecore Customizations for AutoFixture insteadhelp to reduce the amount of FakeDbcode needed to get started. See how the sucesssuccess scenario maycould look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

I'd say FakeDb is outdated and I'd not recommend using it nowadays. At least, after Sitecore 9 where abstractions were introduced, there is no need in it at all.

An alternative approach is to use mocks, AutoFixture and Sitecore Customizations for AutoFixture instead of FakeDb. See how the sucess scenario may look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

I'd say FakeDb is outdated and I'd not recommend using it nowadays. Sitecore 9 brought abstractions so regular mocks can be utilized.

AutoFixture and Sitecore Customizations for AutoFixture help to reduce the amount of code needed to get started. See how the success scenario could look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

Source Link

I'd say FakeDb is outdated and I'd not recommend using it nowadays. At least, after Sitecore 9 where abstractions were introduced, there is no need in it at all.

An alternative approach is to use mocks, AutoFixture and Sitecore Customizations for AutoFixture instead of FakeDb. See how the sucess scenario may look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.