Static Mock Help

2 posts, 0 answers
  1. zach davis
    zach davis avatar
    9 posts
    Member since:
    Jan 2010

    Posted 17 Sep 2010 Link to this post

    Hey everyone,

    I am trying to do some static mocking.  here is my test.
           public void timesheet_processor_tests_just_mock()
               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.


  2. Ricky
    Ricky avatar
    467 posts

    Posted 21 Sep 2010 Link to this post

    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:

    public void timesheet_processor_tests_just_mock()
        var currentDate = DateTime.Now;
        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
  3. DevCraft R3 2016 release webinar banner
Back to Top