Michael McDowell
Michael McDowell

Reputation: 413

Instantiating IOptions<> in xunit

I'm trying to write an xunit test for a class (in a .net Core project) that looks something like:

public Class FoodStore:IFoodStore
{
    FoodList foodItems;

    public FoodStore(IOptions<FoodList> foodItems)
    {
        this.foodItems = foodItems;
    }

    public bool IsFoodItemPresentInList(string foodItemId)
    {
        //Logic to search from Food List
    }
}`

Note: FoodList is actually a json file, containing data, that is loaded and configured in the Startup class.

How can I write an xunit test with appropriate dependency injection to test the IsFoodItemPresentInList method ?

Upvotes: 11

Views: 7362

Answers (6)

Raghavendra Dinavahi
Raghavendra Dinavahi

Reputation: 51

You can do like below: private Mock<IOptions<SomeSettings>> _someSettings;

and in the test method:

_someSettings = new Mock<IOptions<SomeSettings>>();

_someSettings.Setup(o => o.Value).Returns(new SomeSettings
{
    ClientId = "723jsisksokskskos",

    ClientSecret = "sjhs;~jspkasjd;asdj;asdpqwoepqwe",

    ConnectionStrings = new List<ConnectionStrings>
    {
        new ConnectionStrings { Acronym = "A1"  },
        new ConnectionStrings { Acronym = "A2"  }
    }
});

Model structure:

public class SomeSettings : ISomeSettings
{
    public const string ConfigurationName = nameof(SomeSettings);
   
    public string ClientId { get; set; }

    public string ClientSecret { get; set; }

    public string QueryCount { get; set; }

    public List<ConnectionStrings> ConnectionStrings { get; set; }

}

Upvotes: 0

mikesigs
mikesigs

Reputation: 11420

You can create an instance of IOptions<FoodList> using the Options.Create method:

var foodListOptions = Options.Create(new FoodList());

Upvotes: 14

Đorđe Petrović
Đorđe Petrović

Reputation: 179

You could use OptionsWrapper<T> class to fake your configuration. Then you can pass in this object to your class that you want to test. That way you don't have to use DI or read the real configuration.

Something like this:

var myConfiguration = new OptionsWrapper<MyConfiguration>(new MyConfiguration
            {
                SomeConfig = "SomeValue"
            });
var yourClass = new YourClass(myConfiguration);

Upvotes: 12

Ryan O&#39;Neill
Ryan O&#39;Neill

Reputation: 5707

As suggested by the other answers, in your test class you can create an options instance just for testing.

You can do it like this;

public class FakeFoodList : IOptions<FoodList>
{
    public FoodList Value
    {
        get
        {
            return new FoodList(); // TODO: Add your settings for test here.
        }
    }
}

And then call it like this;

var foodOptions = new FakeFoodList();
var foodStore = new FoodStore(foodOptions);

var response = foodStore.Act();

Assert.Equal("whatever", response);

Upvotes: 1

MienDev
MienDev

Reputation: 1869

I have encountered a similar problem (using xUnit), after some struggle, I worked it out.

The answer is so late, but should be helpful for others.


For your Question:

public Class FoodStoreTest
{
    private readonly IConfigurationRoot _configuration;
    private readonly IServiceProvider _serviceProvider;

    public FoodStoreTest(){
            // read Json
            var configBuilder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
            _configuration = configBuilder.Build();

            // SetUp DI
            var services = new ServiceCollection();
            services.AddOptions(); // this statement is required if you wanna use IOption Pattern.

            services.Configure<YuntongxunOptions>(_configuration.GetSection("yuntongxun"));
            _serviceProvider = services.BuildServiceProvider();
    }

    [Fact]
    public void GetFootItemOption()
    {
         IOption<FoodList> optionAccessor = _serviceProvider.GetService<IOptions<FoodList>>();
         FoodList footListOptions = optionAccessor.value;
         Assert.NotNull(footListOptions)
        // ...
    }
}

Also, you should copy "appSettings.json" to your project root folder.

Upvotes: 9

metalheart
metalheart

Reputation: 3809

In a unit test, you typically don't use Dependency Injection, since it's you who controls the creation of the tested object.

To supply a suitable object that implements IOptions<FoodList> you can implement a fake class with the desired behavior yourself, or use some mocking framework to configure the instance on the fly, for example Moq.

Upvotes: 3

Related Questions