.NET Core Unit Testing - Mock IOptions<T>
<p>I feel like I'm missing something really obvious here. I have classes that require injecting of options using the .NET Core <code>IOptions</code> pattern(?). When I unit test that class, I want to mock various versions of the options to validate the functionality of the class. Does anyone know how to correctly mock/instantiate/populate <code>IOptions&lt;T&gt;</code> outside of the Startup class?</p>
<p>Here are some samples of the classes I'm working with:</p>
<p>Settings/Options Model</p>
<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OptionsSample.Models
{
    public class SampleOptions
    {
        public string FirstSetting { get; set; }
        public int SecondSetting { get; set; }
    }
}
</code></pre>
<p>Class to be tested which uses the Settings:</p>
<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OptionsSample.Models
using System.Net.Http;
using Microsoft.Extensions.Options;
using System.IO;
using Microsoft.AspNetCore.Http;
using System.Xml.Linq;
using Newtonsoft.Json;
using System.Dynamic;
using Microsoft.Extensions.Logging;

namespace OptionsSample.Repositories
{
    public class SampleRepo : ISampleRepo
    {
        private SampleOptions _options;
        private ILogger&lt;AzureStorageQueuePassthru&gt; _logger;

        public SampleRepo(IOptions&lt;SampleOptions&gt; options)
        {
            _options = options.Value;
        }

        public async Task Get()
        {
        }
    }
}
</code></pre>
<p>Unit test in a different assembly from the other classes:</p>
<pre><code>using OptionsSample.Repositories;
using OptionsSample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

namespace OptionsSample.Repositories.Tests
{
    public class SampleRepoTests
    {
        private IOptions&lt;SampleOptions&gt; _options;
        private SampleRepo _sampleRepo;


        public SampleRepoTests()
        {
            //Not sure how to populate IOptions&lt;SampleOptions&gt; here
            _options = options;

            _sampleRepo = new SampleRepo(_options);
        }
    }
}
</code></pre>