0

I have controller with DI approach.want to test each action of controller using unit testing. **Controller **

  public SearchController(ILookupSearch lookupSearch, IFullSearch fullSearch, IEmpUow uow, ILogger<SearchController> logger, TelemetryClient telemetryClient, IHostingEnvironment hostingEnvironment, IOptions<ConnectionStringsConfig> connectionString, IOptions<AppSettingsConfig> options)
    {
        this.LookupSearch = lookupSearch;
        this.FullSearch = fullSearch;
        this.EmpUow = uow;
        this.logger = logger;
        this.hostingEnvironment = hostingEnvironment;
        this.connectionString = connectionString.Value;
        this.appSettings = options.Value;
        this.telemetryClient = telemetryClient;
    }


    [Route("Test")]
        [ActionName("Test")]
        [HttpGet]
        public IActionResult Test()
        {
if(this.appsettings.enableDummyData){
            return this.SearchEmpData(new EmpIdQueryField() { Country = "Sweden", EMPID = 441150 });
           }
        }

call goes to controller but all dependencies values in constructor are null.

Unit test case

   public async void Test1()
    {

        //Arrange

        Mock<IHostingEnvironment> hostingEnvironment = new Mock<IHostingEnvironment>();
        TelemetryClient telemetryClient = new TelemetryClient();
        Mock<ILogger<SearchController>> logger = new Mock<ILogger<SearchController>>();
        Mock<AppSettingsConfig> appSettings = new Mock<AppSettingsConfig>();

        Mock<IOptions<ConnectionStringsConfig>> connectionString = new Mock<IOptions<ConnectionStringsConfig>>();
        Mock<IOptions<AppSettingsConfig>> options = new Mock<IOptions<AppSettingsConfig>>();

        Mock<ILookupSearch> lookupSearch = new Mock<ILookupSearch>();
        Mock<IFullSearch> fullSearch = new Mock<IFullSearch>();
        Mock<IOneSoeUow> EmpUow = new Mock<IEmpUow>();

        //Act
        SearchController search = new SearchController(lookupSearch.Object, fullSearch.Object, oneSoeUow.Object, logger.Object, telemetryClient, hostingEnvironment.Object, connectionString.Object, options.Object);
        search.Test();

1 Answer 1

1

In your Test action you are consuming appsettings property, you need to setup appsettings instance in mock object,

    Mock<IOptions<AppSettingsConfig>> options = new Mock<IOptions<AppSettingsConfig>>();
    options.Setup(e=>e.appsettings).Returns(new Appsettings(){ enableDummyData = true });

It will mock enableDummyData to true,

Likewise, you need to setup all other properties of dependencies which you are consuming in this action or in other actions

Sign up to request clarification or add additional context in comments.

1 Comment

Tried the same but got error: 'Invalid setup on a non-virtual (overridable in VB) member: a => a.SWITCH_ENABLE_DUMMY_DATA'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.