Asserting expectations on an object is asserting expectations on other objects?

1 Answer 27 Views
General Discussions
James
Top achievements
Rank 1
James asked on 07 Feb 2025, 08:39 PM | edited on 07 Feb 2025, 08:49 PM
Recently started using JustMock to create unit tests so my knowledge is limited at time. I have the below test code with occurrence expectations on a few arrangements for different objects set. The intention was that the mocked ItemCollection and mocked RadComboBox objects were to be used 3 times to avoid having to create them again, since nothing about the calls change and the mocked SelectionChangedEventArgs object was to be recreated each time with different arrangements.

However, when I assert the expectations on mockSelectionChangedEventArgs, somehow that is also asserting the expectations on itemCollection_Single and radComboBox_Single which both fail because at the time there has only been one call on each of Count and Items. By the end when I do assert the expectations on itemCollection_Single  and radComboBox_Single it would be 3 as in my expectations since I would have used them 3 times.

Why does asserting all expectations on mockSelectionChangedEventArgs assert them on the other two objects? I cannot find any documentation on this scenario. I can avoid this issue by specifically mocking mockSelectionChangedEventArgs.AddedItems and mockSelectionChangedEventArgs.OriginalSource, but I don't see why that is necessary.

            ItemCollection itemCollection_Single = Mock.Create<ItemCollection>(Behavior.Strict);
            Mock.Arrange(() => itemCollection_Single.Count).Returns(2).Occurs(3, nameof(itemCollection_Single.Count));

            RadComboBox radComboBox_Single = Mock.Create<RadComboBox>(Behavior.Strict);
            Mock.Arrange(() => radComboBox_Single.Items).Returns(itemCollection_Single).Occurs(3, nameof(radComboBox_Single.Items));

            mockSelectionChangedEventArgs = Mock.Create<SelectionChangedEventArgs>(Behavior.Strict);
            Mock.Arrange(() => mockSelectionChangedEventArgs.AddedItems).Returns(new List<ParameterValue>() { selectionRuleRow.AllowedValues[0] }).Occurs(2, nameof(mockSelectionChangedEventArgs.AddedItems));
            Mock.Arrange(() => mockSelectionChangedEventArgs.OriginalSource).Returns(radComboBox_Single).Occurs(1, nameof(mockSelectionChangedEventArgs.OriginalSource));

            selectionRuleRow.MultipleEnumSelectionChanged.Execute(mockSelectionChangedEventArgs);

            Assert.AreEqual(expected: "Yes", actual: selectionRuleRow.Value);
            Assert.IsTrue(selectionRuleRow.CanApply);

            Mock.Assert(mockSelectionChangedEventArgs); // ==> fails because of set expectations on itemCollection_Single and radComboBox_Single 
            //Mock.Assert(() => mockSelectionChangedEventArgs.AddedItems, Occurs.Exactly(1));
            //Mock.Assert(() => mockSelectionChangedEventArgs.OriginalSource, Occurs.Exactly(1));

1 Answer, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 12 Feb 2025, 01:22 PM

Hello James,

I am sorry to hear about your negative experience. JustMock has a poorly documented feature that arises when the Returns clause of a particular arrangement uses another mock as a parameter. You can find more details and a solution to the issue here.

Regards,
Ivo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

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