Tuesday, January 5, 2010

My first go at Unit Testing

I've picked up "The Art of Unit Testing  with examples in .net" and I'm almost halfway done reading it. It has provided a very good walkthrough of unit testing and based on what I understand from straight reading, I've implemented some tests using Visual Studio 2008 professional's built in test functionality and RhinoMocks.

       [TestMethod]
        
public void IsValidCopyDictionary_IncludingIdentifier_EqualsReflectionCount()
        {
            
var source = Rhino.Mocks.MockRepository.GenerateStub<IAmAnAssessment>();
            
var destination=Rhino.Mocks.MockRepository.GenerateStub<IAmAnAssessment>();
            
var dictionary = ModelAssessment.ActionDictionary(destination, source, true);
            
var modelProperties=typeof (ModelAssessment).GetProperties();
            
Assert.AreEqual(dictionary.Count, modelProperties.Count(), "CopyDictionary:include is invalid");
        }
        [
TestMethod]
        
public void IsValidCopyDictionary_ExcludingIdentifier_EqualsReflectionCount()
        {
            
var source = Rhino.Mocks.MockRepository.GenerateStub<IAmAnAssessment>();
            
var destination = Rhino.Mocks.MockRepository.GenerateStub<IAmAnAssessment>();
            
var dictionary = ModelAssessment.ActionDictionary(destination, source, false);
            
var modelProperties = typeof(ModelAssessment).GetProperties();
            
Assert.AreEqual(dictionary.Count, modelProperties.Count()-1,"CopyDictionary:exclude is invalid");
        }



        [
TestMethod]
        
public void IsValidCopyDictionary_FullCopy_EqualsReflectionCopy()
        {
            
var source = Rhino.Mocks.MockRepository.GenerateStub<IAmAnAssessment>();
            source.AssessmentIdentifier = 1;
            source.AssociateSeverity =
"1";
            source.ShareholderSeverity =
"3";
            source.CustomerSeverity =
"9";
            source.DefectCode =
"ABC";
            source.InvestorRequirementsIds =
new List<int>() {1, 2, 3};
            source.Nbk =
"NBK1234";
            source.RegulatorySeverity =
"9";
            source.RootCauseIdentifier = 4;
            source.ShareholderSeverity =
"1";
            
var destination = Rhino.Mocks.MockRepository.GenerateStub<IAmAnAssessment>();
            
var dictionary = ModelAssessment.ActionDictionary(destination, source, true);
          
            
var modelProperties = typeof(IAmAnAssessment).GetProperties();
            
foreach (var info in modelProperties)
            {
                dictionary[info.Name].Invoke();
                
Assert.IsNotNull(info.GetValue(destination,null),"Property value is null:"+info.Name);

                
if (info.PropertyType.IsValueType == false)
                    
Assert.AreSame(info.GetValue(destination, null), info.GetValue(source, null),
                        
"Reference type did not copy reference:" + info.Name);

                
if(info.GetIndexParameters().Length==0)
                {
                    
Assert.AreEqual(info.GetValue(source, null), info.GetValue(destination, null),
                        
"PropertyValue is not equal:"+info.Name);
                  
}
                
else
                {
                    
Assert.AreSame(info.GetValue(destination,null),info.GetValue(source,null),"indexed property is not the same:"+info.Name);
                }

            }
          
        }

No comments:

Post a Comment