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;
}
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. Commented Apr 19, 2022 at 7: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<IList<JObject>>(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 Commented Apr 19, 2022 at 7:24
  • I updated, for mocking i was trying to send facke josn methode Commented Apr 19, 2022 at 7: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? Commented Apr 19, 2022 at 8:20
  • Deepak-MSFT I updated the new one code. Commented Apr 19, 2022 at 8:32

1 Answer 1

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.

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

Comments

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.