Mocked object not Mocking properly

4 posts, 0 answers
  1. Chris McNear
    Chris McNear avatar
    12 posts
    Member since:
    Feb 2010

    Posted 13 Feb 2012 Link to this post

    We are using the Q1 2011 release (at least until Q1 2012) is released.  I have a few problems related to objects that should be mocked and arranged not behaving as they are arranged/mocked.  We have a base class that we use for all of our testing within a class in the code, and then each method within code has a TestClass associated with it, with the seperate test methods for that method under test defined within.

    here is the base class
    [TestClass]
       public class SPM150ControllerTestBase
       {
     
           public IAssetsService AssetsServiceMock { get; set; }
           public INavigationService NavigationServiceMock { get; set; }
           public ISPM150Service SPM150ServiceMock { get; set; }
           public IGlobalData GlobalDataMock { get; set; }
           public SPM150Controller Target { get; set; }
     
           [TestInitialize]
           public void TestInit()
           {
               AssetsServiceMock = Mock.Create<IAssetsService>();
               SPM150ServiceMock = Mock.Create<ISPM150Service>();
               NavigationServiceMock = Mock.Create<INavigationService>();
               GlobalDataMock = Mock.Create<IGlobalData>();
               Target = Mock.Create<SPM150Controller>(Behavior.CallOriginal, new object[] { AssetsServiceMock, SPM150ServiceMock, NavigationServiceMock, GlobalDataMock, null });
     
               OnInitialize();
           }
     
           protected virtual void OnInitialize()
           { }
     
       }

    now a class and method that the test is failing on....

    [TestClass]
        public class UpdateEquipmentTest : SPM150ControllerTestBase
        {
            public IEquipmentListItem UpdateItemMock { get; set; }
            public IEquipmentListItem ExistingItemMock { get; set; }
     
            //[TestInitialize]
            //public void OnInit()
            //{
            //    base.TestInit();
            //}
     
            protected override void OnInitialize()
            {
                UpdateItemMock = Mock.Create<IEquipmentListItem>();
                ExistingItemMock = Mock.Create<IEquipmentListItem>();
     
                //Mock.Arrange(() => UpdateItemMock.BenchmarkValue).Returns(100);
                //Mock.Arrange(() => UpdateItemMock.Classification).Returns("No Class");
                //Mock.Arrange(() => UpdateItemMock.DeviceName).Returns("A Device");
                //Mock.Arrange(() => UpdateItemMock.IPAddress).Returns("000.000.000.000");
                //Mock.Arrange(() => UpdateItemMock.Request).Returns(0);
     
                //Mock.Arrange(() => ExistingItemMock.BenchmarkValue).Returns(100);
                //Mock.Arrange(() => ExistingItemMock.Classification).Returns("No Class");
                //Mock.Arrange(() => ExistingItemMock.DeviceName).Returns("A Device");
                //Mock.Arrange(() => ExistingItemMock.IPAddress).Returns("000.000.000.000");
                //Mock.Arrange(() => ExistingItemMock.Request).Returns(0);
     
                UpdateItemMock.BenchmarkValue = 100.0;
                UpdateItemMock.Classification = "No Class";
                UpdateItemMock.DeviceName = "A Device";
                UpdateItemMock.IPAddress = "000.000.000.000";
                UpdateItemMock.ModbusUnitId = 100;
                UpdateItemMock.Request = 0;
     
                //set with default values can change if needed...
                ExistingItemMock.BenchmarkValue = 100.0;
                ExistingItemMock.DeviceName = "A Device";
                ExistingItemMock.ModbusUnitId = 100;
                ExistingItemMock.IPAddress = "000.000.000.000";
                ExistingItemMock.Request = 0;
     
                Mock.Arrange(() => Target.LogBenchmarkValueChange(Arg.AnyString, Arg.AnyDouble, Arg.AnyString, Arg.AnyString, Arg.AnyDouble)).DoNothing();
                Mock.Arrange(() => Target.LogIPChange(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString)).DoNothing();
                Mock.Arrange(() => Target.LogModBusUnitIdChange(Arg.AnyString, Arg.AnyInt, Arg.AnyString, Arg.AnyString, Arg.AnyInt)).DoNothing();
                Mock.Arrange(() => Target.LogDeviceNameChange(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString)).DoNothing();
     
            }
     
     
           
            [TestMethod]
            public void Test0020CheckForSiteAndUserCalledCorrectParametersVersion1()
            {
                Mock.Arrange(() => Target.CheckForSiteNumberAndUserName(Arg.AnyString, Arg.AnyString)).DoNothing().OccursOnce();
     
                Mock.Arrange(() => SPM150ServiceMock.GetEquipmentDetails(Arg.AnyInt)).Returns(ExistingItemMock);
     
     
                Target.UpdateEquipment("12345", UpdateItemMock, "Hi Bob!");
     
                //Target.Assert();
     
                //Target.Assert(x => x.CheckForSiteNumberAndUserName("12345", "Hi Bob!"));
                Mock.Assert(() => Target.CheckForSiteNumberAndUserName("12345", "Hi Bob!"));
            }
    }


    with this particular example we have two issues.
    1. The Call to CheckForSiteNumberAndUserName executes the original code despite the DoNothing on the arrange.
    2. The call to GetEquipmentDetails within the SPM150 Mock, does nothing (as it should) but returns null despite the fact that it is arranged to return the ExistingItemMock (created in the OnInitialize method)

    Any help is greatly appreciated.
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 16 Feb 2012 Link to this post

    Hi Chris,

    Thanks again for reporting the issue. However, i tried it with the latest version (Q1 2012) and it seems to work as expected. I am attaching the project, please check it out and let me know if you still getting the exception.

    Kind Regards,
    Mehfuz
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft R3 2016 release webinar banner
  4. Chris McNear
    Chris McNear avatar
    12 posts
    Member since:
    Feb 2010

    Posted 16 Feb 2012 Link to this post

    I believe we have identified the issue.

    We think that this issue was actually related to some unknown issue with the development environment in use.  We have since wiped and reloaded that machine.  Hopefully it will be resolved from here forward.

    We identified the machine to tbe the issue by running the same unit tests on a different machine without any issues.
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 17 Feb 2012 Link to this post

    Hi Chris,
    It's great to hear that things are working now. Should you face any other problem, please don't hesitate to contact me.

    Kind Regards,
    Mehfuz
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top