Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could look at wrapping all of the environmental settings that could change in a separate class that implements an interface. This could then be passed to the static method you have written which would then call back against the interface when it needs the various settings.</p> <p>This would allow you to mock the environmental settings to standardize when testing to prevent the issue you are seeing. It would also let you vary the settings to test different conditions.</p> <p>e.g.</p> <pre><code>public interface IEnvironment { public bool SupportsIPV6 { get; } } </code></pre> <p>And then your static method becomes.</p> <pre><code>public static void DoSomething(IEnvironment environment) { if(environment.SupportsIPV6) { ... } } </code></pre> <p>Then provide an implementation of IEnvironment that calls against the static System.Net method to get the actual setting for use, but a mock implementation for testing with a known value.</p> <p>If you don't want to pass the interface into the static method you could possibly look at implementing an object using the singleton pattern that then calls into this interface. The static method you have could then use the singleton to access whatever the current interface is although this method is a little messy so I'd probably go with the former.</p> <p>E.g.</p> <pre><code>public Environment : IEnvironment { private static IEnvironment current = new Environment(); public static IEnvironment Current { get { return current; } set { current = value; } } public bool SupportsIPV6 { return System.Net..... } } </code></pre> <p>Then call into it from your static method using.</p> <pre><code>public static void DoSomething(IEnvironemnt environment) { if(Environment.Current.SupportsIPV6) { ... } } </code></pre> <p>And you could change it in your tests via</p> <pre><code>Environment.Current = new MockedEnvironment(); </code></pre> <p>Although you'd need to watch out if you are doing any multithreading with a singleton like that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload