0

I have a fast query :

/sitecore/content/Countries/*

I tried to mock them using nunit as

using (Db db = new Db()
            {

                new DbItem("Countries"){
                    new DbItem("India"){

                 },
                new DbItem("South Africa"){

                 },
                },
            })

But it returns only the empty Item.It is not taking India and Africa.Suggest some ways to Mock those Items.

5

When writing unit tests for Sitecore my general rule of thumb is to try not to test whether it's doing its job, that's not your responsibility.

I think you might be better off abstracting this away behind some interface. From your question, I can only assume that you have some consumer that is using a list of countries in some way. If you define an interface whose responsibility is to get a list of countries.

interface ICountries
{
    IEnumerable<Country> GetAllCountries();
}

class Country
{
    public string Name { get; set; }
}

You can then pass this in as a constructor argument of the consumer

class CountriesConsumer 
{
    ICountries _countries;
    CountriesConsumer(ICountries countries)
    {
        _countries = countries;
    }

    DoSomethign()
    {
        _countries.GetAllCountries();
    }
}

I prefer this approach as then all I need to do is mock my own focused interface and let Sitecore just do its thing.

3

As I stated in comments, it's a good practice to provide a Minimal, Complete, and Verifiable example. For this particular case, it seems everything works fine so I need more details to reproduce your issue. If I create a test like this, it passes (NUnit 3.10.1):

[Test]
public void QueryTest()
{
    using (var db = new Db
    {
        new DbItem("Countries")
        {
            new DbItem("India"),
            new DbItem("South Africa")
        }
    })
    {
        var selectItems = db.Database.SelectItems("fast://sitecore/content/Countries/*");
        Assert.Equal(2, selectItems.Length); // Pass
    }
}

Please take a note that FastQuery support is limited in FakeDb.

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.