Another Unit Testing BO question...

Another Unit Testing BO question...

Old forum URL: forums.lhotka.net/forums/t/4656.aspx


reagan123 posted on Thursday, April 10, 2008

Hi everyone.
I'm new to unit testing and CSLA am currently using NUnit to test my BusinessBase objects.

I'm trying to test the CRUD operations for my object.  My problem is that in the actual application I store the GUID of the logged in user so that anytime an object is saved it records the GUID of the signed in person.

_PIDStamp =
CType(ApplicationContext.User.Identity, Security.Identity).PIDGuid()

The issue is that when I'm running my test, i'm not logged in as anyone and this fails.  Is there a better approach or a good way around this.

Any tips or info would be greatly appreciated.

***P.S: I'd also like to see examples of other peoples tests they use for their CSLA objects if possible.

sergeyb replied on Thursday, April 10, 2008

You can issue a principal login prior to running each test, either in test body or in Test Initialize method, such as MyPrincipal.Login()

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: reagan123 [mailto:cslanet@lhotka.net]
Sent: Thursday, April 10, 2008 10:48 AM
To: Sergey Barskiy
Subject: [CSLA .NET] Another Unit Testing BO question...

 

Hi everyone.
I'm new to unit testing and CSLA am currently using NUnit to test my BusinessBase objects.

I'm trying to test the CRUD operations for my object.  My problem is that in the actual application I store the GUID of the logged in user so that anytime an object is saved it records the GUID of the signed in person.

_PIDStamp = CType(ApplicationContext.User.Identity, Security.Identity).PIDGuid()


The issue is that when I'm running my test, i'm not logged in as anyone and this fails.  Is there a better approach or a good way around this.

Any tips or info would be greatly appreciated.

***P.S: I'd also like to see examples of other peoples tests they use for their CSLA objects if possible.



reagan123 replied on Thursday, April 10, 2008

Seems to work great. Thanks!

Does anyone want to post what a typical test for their business objects may look like or what type of things you test for?

Thanks again!

RikGarner replied on Friday, April 11, 2008

Here's an example of a test I wrote very early on in my project:

[TestFixture]
    public class ReportList_Tests
    {
        [TestFixtureSetUp]
        public void ReportList_Test_Setup()
        {
            TestUtilities.AuthenticateTestUser();
        }

        [Test]
        public void ReportList_IsPopulated()
        {
            ReportList list = ReportList.GetReportList();
            Assert.IsTrue(list.Count > 0, "List is empty");
        }

        [Test]
        public void ReportList_CheckOne()
        {
            ReportList list = ReportList.GetReportList();
            Random selector = new Random();

            int selectNum = selector.Next(list.Count - 1);
            ReportInfo selectedItem = list[selectNum];
         
            ReportEdit fetchedObject = ReportEdit.GetReportEdit(selectedItem.ReportID);

            Assert.IsTrue(selectedItem.ReportType == fetchedObject.ReportType, string.Format("Objects differ {0} {1} {2}", selectedItem.ReportID, selectedItem.ReportType, fetchedObject.ReportType));
            Assert.IsTrue(selectedItem.IsActive == fetchedObject.IsActive, string.Format("Objects differ {0} {1} {2}",selectedItem.ReportID, selectedItem.IsActive , fetchedObject.IsActive));
            Assert.IsTrue(selectedItem.TemplateDocId == fetchedObject.TemplateDocumentId, string.Format("Objects differ {0} {1} {2}", selectedItem.ReportID,selectedItem.TemplateDocId, fetchedObject.TemplateDocumentId));
        }

    }

The tests got a lot more complicated as time went on, for example here is a single test from one fixture:

[Test]
        public void CaseTaskSet_Tests_TestAddTask()
        {
            int testRowID = TestHelper.LocateRandomRow("CASE", "CASE_ID");
            Trace.WriteLine((new StackFrame()).GetMethod().Name + " ID=" + testRowID);
           
            CaseTaskSet targetSet = CaseTaskSet.GetCaseTaskSet(testRowID);

            CaseTaskEdit newItem = CaseTaskEdit.NewCaseTask();
            newItem.CaseID = testRowID;
            newItem.CaseStageID = 1;
            newItem.TaskType = TaskType.Review;
            newItem.TargetDate = new SmartDate(DateTime.Today);
            newItem.Description = "MbUnit test";
            targetSet.Add(newItem);
           
            targetSet.Save();

            int newId = newItem.CaseTaskID;

            _teardownCommands.Add(string.Format("DELETE FROM {0} WHERE {1}={2}", _tableName, _tablePK, newId));
            int rowCount = TestHelper.RowCount(_tableName, string.Format("CASE_ID={0}", testRowID));
            Assert.IsTrue(targetSet.Count == rowCount,string.Format("Added task not found: returned {0} items, table contains {1}", targetSet.Count, rowCount));
        }

 

As you can guess writing all this gets very time consuming very quickly.

reagan123 replied on Friday, April 11, 2008

Thanks so much for the code.  It's good to see what others do.  I've not been able to find many examples of actual tests that are run while searching the internet.

I appreciate your time.

Joffies replied on Monday, April 21, 2008

I have similar question on UNIT testing against CSLA business objects.

I am also in the process of writing unit test using NUnit and I am having an issue testing that a start date property should not be after my end date property.

The validation rules kick in perfectly through databinding but when I set the properties manually through code it does not. The object stays valid. I am not sure if I am missing something.

I am using the exact same function that is used in the Project Tracker sample application.

-------------------------------------------------------

'Start and End Dates

ValidationRules.AddRule(Of Administrator)(AddressOf StartDateGTEndDate(Of Administrator), StartDateProperty)

ValidationRules.AddRule(Of Administrator)(AddressOf StartDateGTEndDate(Of Administrator), EndDateProperty)

'Function to enforce start date should not be greater then end date

Private Shared Function StartDateGTEndDate(Of T As Administrator)( _

ByVal target As T, _

ByVal e As Validation.RuleArgs) As Boolean

If target.GetProperty(Of SmartDate)(StartDateProperty) > target.GetProperty(Of SmartDate)(EndDateProperty) Then

e.Description = "Start date can't be after end date"

Return False

Else

Return True

End If

End Function

-----------------------------------------------------

Thanks

Joffies replied on Tuesday, April 22, 2008

Any ideas as I really want to get this working and I cannot find anything wrong or indicating what could be wrong.

Thanks

RikGarner replied on Tuesday, April 22, 2008

Is there some quirk that would cause your test code not to eventually trigger the PropertyHasChanged call inside the Property Set routines?

 

sergeyb replied on Tuesday, April 22, 2008

I see no reason why the rules would not fire if you set a date on object created in code.  If you are using VS unit testing, you can debug your tests and set breakpoints on PropertyHasChanged(Date) and inside the rule itself.  This way you should be able to tell what is going on.

 

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

Magenic ®

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: Joffies [mailto:cslanet@lhotka.net]
Sent: Tuesday, April 22, 2008 8:12 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Another Unit Testing BO question...

 

Any ideas as I really want to get this working and I cannot find anything wrong or indicating what could be wrong.

Thanks



RikGarner replied on Tuesday, April 22, 2008

Post your test code, or a bit of it at least.

Joffies replied on Tuesday, April 22, 2008

Hi Rik

Here is the test. I am expecting the object not to be saveable due to the validation rules on the object not being met.

Both endDate and startDate values are current as in set to today.

<Test()> _

Public Sub TestDateValidation()

Dim adm As Administrator = Administrator.GetAdministrator(_administratorID)

adm.EndDate = _endDate.AddDays(-2).ToString

adm.StartDate = _startDate.ToString

Assert.IsFalse(adm.IsSavable, "End Date cannot be before the start date")

adm.Save()

End Sub

sergeyb replied on Tuesday, April 22, 2008

What are the values for both dates at the Assert line?  If you want try hard values, try this:

adm.EndDate = “1/1/2002”

adm.StartDate = “1/1/2003”

 

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: Joffies [mailto:cslanet@lhotka.net]
Sent: Tuesday, April 22, 2008 9:13 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: RE: Another Unit Testing BO question...

 

Hi Rik

Here is the test. I am expecting the object not to be saveable due to the validation rules on the object not being met.

<Test()> _

Public Sub TestDateValidation()

Dim adm As Administrator = Administrator.GetAdministrator(_administratorID)

adm.EndDate = _endDate.AddDays(-2).ToString

adm.StartDate = _startDate.ToString

Assert.IsFalse(adm.IsSavable, "End Date cannot be before the start date")

adm.Save()

End Sub



Joffies replied on Tuesday, April 22, 2008

Thanks Sergey.

I did check my values and as it is I have made a schoolboy error. :(

Apologies. I have set private readonly variables within my test which i purposfully set and did not check. Apologies and thanks for your help.

Copyright (c) Marimer LLC