Hacker News new | ask | show | jobs
by ghubbard 4865 days ago
Trying to unit test the internet is not a good idea, your life will be easier if you refactor things and simplify a little.

  public bool StringIsEvenLength(string somestring )
  public string FetchRemoteString( string uri )

  public bool RemoteStringIsEvenLength(string uri)
  {
      fetched = FetchRemoteString( uri );
      if( fetched != null ) {
        return StringIsEvenLength( fetched );
      }
      throw new SomeOtherException("Didn't get a string to check the length of.");
  }
Now StringIsEvenLength is trivial to test. When you want to test RemoteStringIsEvenLength

You can mock out FetchRemoteString and don't even have to use a WebClient at all.

1 comments

That's a pretty good point, and a much better solution to the example I gave. It'd be interesting to see FetchRemoteString as a Func<string, string> property where you could have a default implementation (web client) and then in your test setup you could just redefine the property to do whatever you want.

The general point still stands though - if there was an interface, we wouldn't have to come up with these workarounds.