How to mock IOptionsSnapshot instance for testing

You should be able to mock up the interface and create an instance of the options class for the test. As I am unaware of the nested classes for the options class I am making a broad assumption.

Documentation: IOptionsSnapshot

//Arrange
//Instantiate options and nested classes
//making assumptions here about nested types
var options = new AbOptions(){
    cc = new cc {
        D1 = "https://",
        D2 = "123145854170887"
    }
};
var mock = new Mock<IOptionsSnapshot<AbOptions>>();
mock.Setup(m => m.Value).Returns(options);

var service = new AbClass(mock.Object);

Access to the nested values should now return proper values instead of NRE


A generic way:

    public static IOptionsSnapshot<T> CreateIOptionSnapshotMock<T>(T value) where T : class, new()
    {
        var mock = new Mock<IOptionsSnapshot<T>>();
        mock.Setup(m => m.Value).Returns(value);
        return mock.Object;
    }

usage:

var mock = CreateIOptionSnapshotMock(new AbOptions(){
    cc = new cc {
        D1 = "https://",
        D2 = "123145854170887"
    }
});