0

I am using free version of JustMock to mock some external dependencies like GetUserSettingsResponse which is a sealed class. The problem is that the free version does not allow us to mock sealed classes. I cannot use the full version because of some reasons. Below is my code sample:

public GetUserSettingsResponse GetUserSettingsResponse(string emailAddress)
{
    var response = service.GetUserSettings(
        emailAddress,
        UserSettingName.ExternalEwsUrl,
        UserSettingName.InternalEwsUrl,
        UserSettingName.EwsSupportedSchemas);
        return response;
}

This is the method I am trying to mock as below:

[TestMethod]
public void GetExchangeService()
{
    //Arrange            
    string url = "";
    GetUserSettingsResponse response;
    ObjectOfMyClass.Arrange(x => x.GetUserSettingsResponse(Arg.IsAny<string>())).Returns(new GetUserSettingsResponse()); //this is where it throws exception saying that only non-sealed classes can be mocked with lite version of just mock

}

Edit: My application is a Web service which is basically using EWS managed APIs to get the email account details from the exchange server. So, in order to start the communication I am first doing the AutoDiscovery to get the url out of email address of the user. So the subject under test is below method which internally calls GetUserSettingsResponse method:

public class MyClass
{
    public ExchangeService GetExchangeService(
                string userName,
                string password,
                int version,
                string emailAddress,
                string userId)
    {
        AutoDiscoverService service = new AutodiscoverService();
        service.UseDefaultCredentials = false;
        service.Credentials = new NetworkCredential(userName, password);
        service.EnableScpLookup = true;

        service.ReturnClientRequestId = true;

        service.PreAuthenticate = false;

        service.RedirectionUrlValidationCallback = RedirectionUrlValidationCallback;

        var url = string.Empty;
        var response = GetUserSettingsResponse(emailAddress);
        if (response.TryGetSettingValue(UserSettingName.ExternalEwsUrl, out settingValue)
            || response.TryGetSettingValue(UserSettingName.InternalEwsUrl, out settingValue))
        {
            ewsurl = settingValue;
        }
        var exchangeService = new ExchangeService((ExchangeVersion)version)
                                      {
                                          Credentials = new WebCredentials(userName, password),
                                          KeepAlive = true,
                                          Url = new Uri(ewsurl)
                                      };
        return exchangeService;
    }
}
tavier
  • 1,744
  • 4
  • 24
  • 53
  • 1
    Coupling to implementation concerns make it difficult to replace them when trying to test in isolation. Abstract implementation concerns behind interface and abstraction that you have control over. That way you are not confined to the limitations imposed by 3rd party dependencies. This should be an indicator that some solid refactoring is in order to make your code clean. – Nkosi Jan 23 '17 at 12:09
  • How do I separate the implementation and write an interface in this scenario? It would be great if you can throw some examples. – tavier Jan 23 '17 at 12:22
  • You need to provide more context information about the subject under test. based on the current code I can only guess it has something to do with exchange. – Nkosi Jan 23 '17 at 12:29
  • @Nkosi I have edited my post with more info. – tavier Jan 23 '17 at 12:39
  • Now to clarify, are you trying to hit the actual service or are you trying to test that method in isolation? 'cause that would change this from a unit test to an integration test. A whole new topic – Nkosi Jan 23 '17 at 12:41
  • No, I am not trying to hit the actual service (autodiscovery). Thats why I am trying to Arrange GetUserSettingsResponse method to return some dummy response. – tavier Jan 23 '17 at 12:54
  • Just for reference take a look at this documentation https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservice(v=exchg.80).aspx – Nkosi Jan 23 '17 at 13:50
  • Code is missing from the SUT – Nkosi Jan 23 '17 at 13:53
  • @Nkosi sorry for the delayed response, I have added the missing return statement from the SUT. – tavier Jan 24 '17 at 05:06
  • What edition of visual studio do you use? – zaitsman Mar 01 '17 at 10:25

0 Answers0