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

Mocked Method not returning correct object

1 Answer 174 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 22 Oct 2012, 01:57 PM
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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Kaloyan
Telerik team
answered on 24 Oct 2012, 02:33 PM
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.

Tags
General Discussions
Asked by
Chris
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or