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

Parameter matching for class based on collection

5 Answers 32 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 30 May 2014, 06:05 PM
[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

5 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 04 Jun 2014, 06:42 AM
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.

 
0
Eric
Top achievements
Rank 1
answered on 04 Jun 2014, 11:07 AM
Thanks.
I saw the other behavior as well, but don't think it is a good long-term solution.

Eric
0
Kaloyan
Telerik team
answered on 04 Jun 2014, 11:12 AM
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.

 
0
Eric
Top achievements
Rank 1
answered on 23 Jul 2014, 11:48 AM
Any updates on this issue.  It still seems to exist in the current version of the product.

Thanks,
Eric

0
Stefan
Telerik team
answered on 24 Jul 2014, 06:57 AM
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.

 
Tags
JustMock Free Edition
Asked by
Eric
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Eric
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or