Tuesday, February 19, 2013

//WE test this by examining the criteria that "DDLForStaff" was called with[Test]
public void TestMethod1_criteriaWithPropertyOfCtrIdGreaterThan0(){

// Test created by powershell on 02/15/2013 14:32:30//*** Arrange***StaffProfilesRptVM TestCriteria = new StaffProfilesRptVM() { CtrId = 15 };


//*** BLL and DAL Stubs ***mockSelfTestDALHdl.Stub(x => x.DDLForStaff(TestCriteria)).IgnoreArguments().Return(new List<DDLDispValueCV>(){});

//*** Act ***selfTestBLLHdl.TestMethod1(TestCriteria);
IList<object[]> args = mockSelfTestDALHdl.GetArgumentsForCallsMadeOn(db => db.DDLForStaff(Arg<StaffProfilesRptVM>.Is.Anything));StaffProfilesRptVM finalVal =
new StaffProfilesRptVM();
if (args != null && args.Count > 0){
finalVal = (StaffProfilesRptVM)(args[0][0]);
}

//*** Assert ***Assert.AreEqual(finalVal.CtrId, 22);// Even though i set the ctr_id as 15, it should be called with 22, if i entered this if cluase.
}

Friday, February 8, 2013

Examples/ Cheatsheet of several common Unit Test sceneriao with Rhinomocks at JCDC

Examples/ Cheatsheet of several common Unit Test sceneriao with Rhinomocks at JCDC
//This test is to demonstrate "return values" from bll by taking different branches[Test]
public void BllMethod1_TestFirstBranchByPassingSam(){

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM(){FirstName = "Sam"};
     Int64 returnValue = 0;
     // Add Stubs for other DAL methods!     callPersonDALHdl.Stub(x => x.DalMethod1(criteria)).IgnoreArguments().Return(100);

     //*** Act ***     returnValue = callPersonBLLHdl.BllMethod1(criteria);

     //*** Assert ***      Assert.AreEqual(returnValue,1);
}


[Test]
public void BllMethod1_TestSecondBranchByPassingMad() {

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM(){FirstName = "Mad"};
     Int64 returnValue = 0;
     // Add Stubs for other DAL methods!     callPersonDALHdl.Stub(x => x.DalMethod2(criteria)).IgnoreArguments().Return(100);

     //*** Act ***     returnValue = callPersonBLLHdl.BllMethod1(criteria);

     //*** Assert ***      Assert.AreEqual(returnValue, 2);
}


[Test]
public void BllMethod1_VerifyDalMethod1IsCalled(){

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM() { FirstName = "Sam" };

     // Add Stubs for other DAL methods!     callPersonDALHdl.Stub(x => x.DalMethod1(criteria)).IgnoreArguments().Return(100);

     //*** Act ***     callPersonBLLHdl.BllMethod1(criteria);

     //*** Assert ***      callPersonDALHdl.AssertWasCalled(x => x.DalMethod1(null),a=>a.IgnoreArguments());}


[Test]

public void BllMethod1_VerifyDalMethod2IsCalled(){

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM() { FirstName = "Mad" };
     // Add Stubs for other DAL methods!     callPersonDALHdl.Stub(x => x.DalMethod2(criteria)).IgnoreArguments().Return(100);

     //*** Act ***     callPersonBLLHdl.BllMethod1(criteria);

     //*** Assert ***      callPersonDALHdl.AssertWasCalled(x => x.DalMethod2(null), a => a.IgnoreArguments());}



// Demonstrating how to check for expected exceptions[Test, ExpectedException(typeof(RulesException))]  // Decorate like this to check for a single specific exceptionpublic void BllMethod1_RulesException()
{

//*** Arrange***          SearchCallPersonVM criteria = new SearchCallPersonVM() { FirstName = "" };
     //*** Act ***     callPersonBLLHdl.BllMethod1(criteria);
}


//Demonstrating "With what parameters the dal call was made"[Test]
public void BllMethod1_TestingDalParamtersWereCorrectlyPassedIn(){

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM() { FirstName = "Sam" };
     // Add Stubs for other DAL methods!     callPersonDALHdl.Stub(x => x.DalMethod2(criteria)).IgnoreArguments().Return(100);

     //*** Act ***     callPersonBLLHdl.BllMethod1(criteria);

     IList<object[]> args = callPersonDALHdl.GetArgumentsForCallsMadeOn(db => db.DalMethod1(Arg<SearchCallPersonVM>.Is.Anything));     SearchCallPersonVM finalVal =
new SearchCallPersonVM();
     if (args != null && args.Count > 0)     {
          finalVal = (SearchCallPersonVM)(args[0][0]);
     }

     // assert.areequal(obj,obj) is not working well since we did not override the equal method     Assert.AreEqual(finalVal.FirstName, "Sam");     Assert.AreEqual(finalVal.LastName,
"Brown");}



//Demonstrating "How to check the number of times a method was called"[Test]
public void BllMethod2_RepeatCalls(){

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM() { FirstName = "Mad" };
     // Add Stubs for other DAL methods!     callPersonDALHdl.Stub(x => x.DalMethod2(criteria)).IgnoreArguments().Return(100);

     //*** Act ***     callPersonBLLHdl.BllMethod2(criteria);

     //*** Assert ***      callPersonDALHdl.AssertWasCalled(c => c.DalMethod1(null), options => options.IgnoreArguments().Repeat.Times(4));}


[Test]

public void BllMethod3_MultipleRulesException(){

     //*** Arrange***     SearchCallPersonVM criteria = new SearchCallPersonVM(){FirstName = "John"};
     try     {
          //*** Act ***          callPersonBLLHdl.BllMethod3(criteria);
     }
     catch(RulesException ex)     {

          if (ex.GetType() == typeof(RulesException))          {
               ErrorInfo[] errors = ((RulesException)ex).Errors.ToArray();
               Assert.AreEqual(errors[0].ErrorMessage,
"Name cannot be John");    
          }
     }