I'm going to do everything else in here with commented code instead of text. It's just easier.
public static Dictionary<string, UrlTarget>
_urlTargetLookup = new Dictionary<string, UrlTarget>();
public static string BuildUrl(string destination, string displayText, string target)
{
//
check whether the lookup dictionary is null or empty
       if (_urlTargetLookup
== null || !_urlTargetLookup.Any())
      
{
       // if it is null or empty, populate it from the
database
       // this is where our unit testing problem comes
into play
       // we don't want to rely on the database results
for our tests to pass
       // we're assuming that the process of getting the
values from the
       // database is not conducive to mocking (there
are real cases
       // where this happens)
      
_urlTargetLookup = GetValuesFromDatabase();
      
}
//
lookup the target in the dictionary and get the enum value
       var targetString
= _urlTargetLookup[target].ToString();
       // build the url
       var urlBuilder
= new StringBuilder();
      
urlBuilder.Append(");
      
urlBuilder.Append(destination);
      
urlBuilder.Append("\"
target=\"");
      
urlBuilder.Append(targetString);
      
urlBuilder.Append("\">");
      
urlBuilder.Append(displayText);
      
urlBuilder.Append("
");
       // return the url
       return urlBuilder.ToString();
}
public List<Endpoint> GetEndpoints()
{
var endpoints = new List<Endpoint>
      
{
      
       new Endpoint
             
{
             
       Name = "Google",
                    
Url = UrlHelper.BuildUrl("http://www.google.com", "Google", "parent")
},
              new Endpoint
             
{
             
       Name = "Yahoo!",
                    
Url = UrlHelper.BuildUrl("http://www.yahoo.com", "Yahoo!", "self")
},
              new Endpoint
             
{
             
       Name = "Bing",
                    
Url = UrlHelper.BuildUrl("http://www.bing.com", "A Better Search Engine", "Blank")
}
};
       return endpoints;
}
[TestMethod]
public void GetEndpointsShouldReturnThreeValidUrls()
{
//
arrange
       var resources
= new Resources();
       // create the ShimsContext in order to use Shims
later
       using (ShimsContext.Create())
      
{
// UrlHelper is a static class that contains a
public static field we need to set in order to mimic a dependency injection
              // create an "instance" of this static
class and use this new instance for testing
              var staticType
= typeof (UrlHelper);
              ConstructorInfo ci = staticType.TypeInitializer;
              var parameters
= new object[0];
             
ci.Invoke(null, parameters);
              // create what amounts to a mocked Dictionary
object
              var endpointMapping
= new Dictionary<string, UrlTarget>
             
{
             
       {"_parent", UrlTarget._parent},
                    
{"parent", UrlTarget._parent},
                    
{"_self", UrlTarget._self},
                    
{"self", UrlTarget._self},
                    
{"_blank", UrlTarget._blank},
                    
{"Blank", UrlTarget._blank},
                    
{"blank", UrlTarget._blank},
                    
{"_top", UrlTarget._top},
                    
{"top", UrlTarget._top}
};
// use reflection to set the public static field
in our instance of the static class so we can be certain what it will contain
when we test
              var dictionaryField
= staticType.GetField("_urlTargetLookup");
             
dictionaryField.SetValue(staticType, endpointMapping);
              // act
              var resourcesResults
= resources.GetEndpoints();
// assert
              Assert.AreEqual(3, resourcesResults.Count);
}
}
 
No comments:
Post a Comment