Mocked Method not returning correct object

2 posts, 1 answers
  1. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 22 Oct 2012 Link to this post

    I have a unit test that is attempting to test a very simple method.  The method under test simply calls two other methods (interface methods that are marked as virtual in the implementation).  I use the following code in the unit test....
    IEnumerable<byte> passedMessage1 = new byte[16];
     
                Mock.Arrange(() => MessageOperationsMock.InsertNetworkAddressIntoMessage
                    (Target.PrimaryReportingAddress, passedMessage1, 3))
                    .DoNothing()
                    .Returns(passedMessage1)
                    .OccursOnce();
     
                Mock.Arrange(() => MessageOperationsMock.InsertNetworkAddressIntoMessage
                    (Target.AlternateReportingAddress, passedMessage1, 25))
                    .DoNothing()
                    .Returns(passedMessage1)
                    .OccursOnce();
     
                Target.PrimaryReportingAddress = LocalTestScenarios.TestIPAddress1;
                Target.AlternateReportingAddress = LocalTestScenarios.TestIPAddress2;
     
                Target.InsertNetworkAddressesIntoPart(passedMessage1);
     
                Mock.Assert(MessageOperationsMock);


    The Target method is....
    internal virtual IEnumerable<byte> InsertNetworkAddressesIntoPart(IEnumerable<byte> messageBeingCreated)
            {
                messageBeingCreated = MessageOperationsHelper.InsertNetworkAddressIntoMessage(PrimaryReportingAddress, messageBeingCreated, 3);
                messageBeingCreated = MessageOperationsHelper.InsertNetworkAddressIntoMessage(AlternateReportingAddress, messageBeingCreated, 25);
     
                return messageBeingCreated;
            }

    and here is the mocking setup....

    protected HardCodedMonitoringSettingsEntity Target;
            protected IDependencyInjection InjectorMock;
            protected IValidationOperations ValidatorMock;
            protected IMessageOperations MessageOperationsMock;
     
            [SetUp]
            public void InitializeBase()
            {
                Target = Mock.Create<HardCodedMonitoringSettingsEntity>(Behavior.CallOriginal);
                InjectorMock = Mock.Create<IDependencyInjection>();
                ValidatorMock = Mock.Create<IValidationOperations>();
                MessageOperationsMock = Mock.Create<IMessageOperations>();
     
                Target.DependencyInjector = InjectorMock;
                Target.ValidationOperationsHelper = ValidatorMock;
                Target.MessageOperationsHelper = MessageOperationsMock;
            }


    The problem is the test says that expected called once but was called 0 times.  What I see when I debug the code is that my passedMessage variable goes from a byte array to some other type of object (goes from saying byte[16] to count = 0 after the first method is called.

    Any thoughts?
  2. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 24 Oct 2012 Link to this post

    Hello Chris,

     Thank you for contacting Telerik support.

     There is one thing I was able to notice in your test method:
    Mock.Arrange(() => MessageOperationsMock.InsertNetworkAddressIntoMessage
                    (Target.PrimaryReportingAddress, passedMessage1, 3))
                    .DoNothing()
                    .Returns(passedMessage1)
                    .OccursOnce();
      
                Mock.Arrange(() => MessageOperationsMock.InsertNetworkAddressIntoMessage
                    (Target.AlternateReportingAddress, passedMessage1, 25))
                    .DoNothing()
                    .Returns(passedMessage1)
                    .OccursOnce();
     You are using the "DoNothing()" method before "Returns()". We do not recommend this approach anymore. Using the newest version of JustMock, your test should look like this:
    Mock.Arrange(() => MessageOperationsMock.InsertNetworkAddressIntoMessage
                    (Target.PrimaryReportingAddress, passedMessage1, 3))
                    .Returns(passedMessage1)
                    .OccursOnce();
      
                Mock.Arrange(() => MessageOperationsMock.InsertNetworkAddressIntoMessage
                    (Target.AlternateReportingAddress, passedMessage1, 25))
                    .Returns(passedMessage1)
                    .OccursOnce();
     The "DoNothing()" method is applicable only for methods without a return value and property sets. You can read more about it here:
     - http://www.telerik.com/help/justmock/basic-usage-mock-do-nothing.html

     Also, I would recommend you to update your JustMock references inside your solution.

     Let me know if this solves your problem.

    Kind regards,
    Kaloyan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top