Parameter matching for class based on collection

6 posts, 0 answers
  1. Eric
    Eric avatar
    3 posts
    Member since:
    May 2014

    Posted 30 May 2014 Link to this post

    [TestFixture]
    public class Class1
    {
        public class Individual
        {
             
        }
        public class Family : Collection<Individual>
        {
             
        }
        public interface ITest
        {
            int Test(Family family);
        }
     
        [Test]
        public void Test()
        {
            var family1 = new Family();
            var family2 = new Family();
     
            var test = Mock.Create<ITest>();
            Mock.Arrange(() => test.Test(family1)).Returns(1).MustBeCalled();
            Mock.Arrange(() => test.Test(family2)).Returns(2).MustBeCalled();
     
            var result = test.Test(family1) + test.Test(family2);
     
            Mock.Assert(test);
        }
    }
    In the code as written I get an error:
    Occurrence expectation failed. Expected at least 1 call. Calls so far: 0
    Arrange expression: () => test.Test(family1).

    However, when I change the code to not have Family based on Collection<Individual> it works.

    This does not seem correct.  Is this a bug, or am I missing something on how JustMock is matching these parameters?

    Eric Gurney
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 04 Jun 2014 Link to this post

    Hello Eric,

    This seems like a bug on our side, as in this scenario we do not differ the parameters passed in both the arrangements. I will log it in our bug tracking system and will make sure it is fixed for future releases.

    Further, I found out that everything works as expected after there are some collection members added to the family1 and 2 arguments, like this:
    [TestMethod]
    public void Test()
    {
        var family1 = new Family();
        family1.Add(new Individual());
        var family2 = new Family();
        family2.Add(new Individual());
     
        var test = Mock.Create<ITest>();
        Mock.Arrange(() => test.Test(family1)).Returns(1).MustBeCalled();
        Mock.Arrange(() => test.Test(family2)).Returns(2).MustBeCalled();
     
        var result1 = test.Test(family1) + test.Test(family2);
     
        Mock.Assert(test);
        Assert.AreEqual(3, result1);
    }

    I hope this helps. Please, let us know if there is anything else we can help with.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft R3 2016 release webinar banner
  4. Eric
    Eric avatar
    3 posts
    Member since:
    May 2014

    Posted 04 Jun 2014 in reply to Kaloyan Link to this post

    Thanks.
    I saw the other behavior as well, but don't think it is a good long-term solution.

    Eric
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 04 Jun 2014 Link to this post

    Hi again Eric,

    You are right about that. That is why we will do our best to apply a fix as soon as possible.

    Thank you for the understanding in advance and sorry for any inconveniences caused.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Eric
    Eric avatar
    3 posts
    Member since:
    May 2014

    Posted 23 Jul 2014 in reply to Kaloyan Link to this post

    Any updates on this issue.  It still seems to exist in the current version of the product.

    Thanks,
    Eric

  7. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 24 Jul 2014 Link to this post

    Hi Eric,

    JustMock compares collections element-wise instead of by reference as a courtesy. On second thought, that only makes sense for collections that can have no additional logic to them, like arrays. In particular, it doesn't make sense for user-defined classes deriving from IEnumerable. I will reopen the bug and it should be fixed in one of our next releases.

    As a temporary workaround I can suggest that you use ReferenceEquals in a matcher, like so:
    Mock.Arrange(() => test.Test(Arg.Matches(c => ReferenceEquals(c, family1)))).Returns(1).MustBeCalled();

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft R3 2016 release webinar banner