This is a migrated thread and some comments may be shown as answers.

Static Mock Help

1 Answer 81 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
zach davis
Top achievements
Rank 1
zach davis asked on 17 Sep 2010, 05:25 PM
Hey everyone,

I am trying to do some static mocking.  here is my test.
[Test]
       public void timesheet_processor_tests_just_mock()
       {
           Mock.SetupStatic(typeof(LaborManager));
           Mock.Arrange(() => LaborManager.GetTimesheetLabor(DateTime.Now, 1683)).Returns(new TimesheetLaborReadOnlyCollection());
           var processor = new TimesheetProcessor();
           processor.ProcessUserTimesheet(DateTime.Now, 1683);
           Mock.Assert(() => LaborManager.GetTimesheetLabor(DateTime.Now, 1683), Occurs.AtLeastOnce());
       }

The class TimesheetProcessor uses LaborManager.GetTimesheetLabor() method which I have setup to return a new Collection which by Default would have a count == 0. 

However, when I step into this and get into the TimesheetProcessor class where LaborManager.GetTimeSheetLabor() method is called.  the variable laborEntries which calls the method I arranged in my test gets set to null instead of a new collection.  Please see the timesheet processor code below

//Timesheet Processor Class
        public void ProcessUserTimesheet(DateTime weekEndingDate, int userId)
        {
            TimesheetsCreated = new List<int>();
            TimesheetLaborReadOnlyCollection laborEntries = LaborManager.GetTimesheetLabor(weekEndingDate, userId);
            if(laborEntries.Count > 0)
                ProcessTimesheetLaborUser(userId, weekEndingDate, laborEntries);
        }

Why is this happening?
Have I setup my test incorrectly?

I have JustMock enabled.

Thanks,

-zd

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 21 Sep 2010, 10:08 AM
Hi zach,

Thank you for submitting the issue. However, DateTime is a value type. Therefore, the DateTime.Now that is passed in ProcessUserTimeSheet and the DateTime.Now that is used to assert the call contains different value.

I updated the test code in the following way and the test is passing as expected:

[Test]
public void timesheet_processor_tests_just_mock()
{
    var currentDate = DateTime.Now;
    Mock.SetupStatic(typeof(LaborManager));
    Mock.Arrange(() => LaborManager.GetTimesheetLabor(currentDate, 1683)).Returns(new TimesheetLaborReadOnlyCollection());
    var processor = new TimesheetProcessor();
    processor.ProcessUserTimesheet(currentDate, 1683);
    Mock.Assert(() => LaborManager.GetTimesheetLabor(currentDate, 1683), Occurs.AtLeastOnce());
}


I also attached the test project to let you have a look. Please feel free to write back if you are facing further issues.


Kind Regards,
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
zach davis
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or