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

Mocked object not Mocking properly

3 Answers 55 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris McNear
Top achievements
Rank 1
Chris McNear asked on 13 Feb 2012, 04:44 PM
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.

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 16 Feb 2012, 01:40 PM
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 >>
0
Chris McNear
Top achievements
Rank 1
answered on 16 Feb 2012, 04:33 PM
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.
0
Ricky
Telerik team
answered on 17 Feb 2012, 12:38 PM
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 >>
Tags
General Discussions
Asked by
Chris McNear
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Chris McNear
Top achievements
Rank 1
Share this question
or