0

My service class which have interface and injected service.

public class SomeService
{
    private readonly ICustomConfigManager _customConfigManager;
    private readonly IList<DefaultRoleSetting> _defaultRoleSettings;
    public FCCLicenseTrackerService(ICustomConfigManager customConfigManager)
    {            
        _customConfigManager = customConfigManager;
        _defaultRoleSettings = customConfigManager.DefaultRoleSettings;
    }

My service method to which I have to make unit test case.

    public async Task<List<CustomModel>> GetRequests(string Id)
    {
        var defaultRoleSetting = _defaultRoleSettings.FirstOrDefault(roleSetting => roleSetting.RoleId == Guid.Parse(GroupHelper.Name));
        ValidateDefaultSettings(defaultRoleSetting);
        return customReq;
    }
   
    private void ValidateDefaultSettings(DefaultRoleSetting defaultRoleSetting)
    {
        if (defaultRoleSetting == null)
        {
            throw new ArgumentException($"{nameof(defaultRoleSetting)} is null");
        }
        if (string.IsNullOrWhiteSpace(defaultRoleSetting.Name))
        {
            throw new ArgumentException($"{nameof(defaultRoleSetting.Name)} is null or empty");
        }
        if (string.IsNullOrWhiteSpace(defaultRoleSetting.Email))
        {
            throw new ArgumentException($"{nameof(defaultRoleSetting.Email)} is null or empty");
        }
    }

}

I am new to unit test case, as I gone through on web, didn't get any solution which is mock internal method, so due to it I am struck when 'defaultRoleSetting' is null pass. So any one can share sample code to mock for 'var defaultRoleSetting'.

My test code is

public async Task GetRequests_SendingValidId_ExpectedReturnObjectType()
{
    // Arrange             
    var fakeDefaultReq = FakeDefaultRoleSetting();
    _mockCustomConfigManager.Setup(s => s.DefaultRoleSettings).Returns(fakeDefaultReq);

    var service = this.CreateService();
    string Id = "2";
    var expected = JsonConvert.DeserializeObject<IList<JObject>>(GetFakeRequests());
    //Act
    var result = await service.GetRequests(Id);

    // Assert
    Assert.Equal(JsonConvert.SerializeObject(result), JsonConvert.SerializeObject(expected));
}

Fake object method for mock dependency injection.

private List<DefaultRoleSetting> FakeDefaultRoleSetting()
{
    string fakeData = @"[{
'RoleId': '00000000-0000-0000-0000-000000000000',
'Name': null,
'Email': null,
'UserId': '00000000-0000-0000-0000-000000000000'
}]";
    return JsonConvert.DeserializeObject<List<DefaultRoleSetting>>(fakeData);
}

Fake object method for return match.

private string GetFakeRequests()
{
    string fakeData = @"[{
'userId': '00000000-0000-0000-0000-000000000000',
'StatusId': 0.0,
'createdOn': '0001-01-01T00:00:00',
'createdBy': '00000000-0000-0000-0000-000000000000',
'modifiedOn': null,
'modifiedBy': null,
'buildoutCompletedDate': null,
'buildoutDeadlineReason': null
}]";
    return fakeData;
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Ahmed
  • 115
  • 7
  • Instead of trying to mock an internal method, pass that method as an `Action` or `Func` argument, either in the method itself or the class's constructor. Another, more traditional option is to extract the validation into a separate class. – Panagiotis Kanavos Apr 19 '22 at 07:19
  • It's unclear to me. Is your unit test in a dedicated project or is it part of the main project ? What is this var expected = JsonConvert.DeserializeObject>(GetRequests()); Also it seems you inject _defaultRoleSettings in you code. You should inject it from you unit test after setting it up for the test... I need more code to understand what you do: Class containing the unit test, how you inject _defaultRoleSettings – Cyril ANDRE Apr 19 '22 at 07:24
  • I updated, for mocking i was trying to send facke josn methode – Ahmed Apr 19 '22 at 07:46
  • Could you please inform us, where you are setting the `defaultRoleSetting` in the JSON code or anywhere else? What are the possible values of it? – Deepak-MSFT Apr 19 '22 at 08:20
  • Deepak-MSFT I updated the new one code. – Ahmed Apr 19 '22 at 08:32
  • Are you using Moq? It seems you are. But I don't see it in the tags, nor the question body. _Side note: `Assert.Equal` should be used with the expected value as first parameter, and the to be tested value as the second._ – JHBonarius Apr 19 '22 at 09:49
  • [1](https://stackoverflow.com/a/63503831/5045688), [2](https://stackoverflow.com/a/68959562/5045688) – Alexander Petrov Apr 19 '22 at 15:04

1 Answers1

0

Actually I was sending dummy data in 'FakeDefaultRoleSetting' json method, when I convert it to real data, the issue gone. Thanks all for support.

Ahmed
  • 115
  • 7