Parameter matching

5 posts, 0 answers
  1. Russell
    Russell avatar
    76 posts
    Member since:
    Sep 2006

    Posted 09 Feb 2013 Link to this post

    If I'm have a class as a parameter to a mocked method, what does JustMock use to determine whether the parameters match or not?

    e.g.

    public class Bi
    {
        protected bool Equals(Bi other)
        {
            return I == other.I;
        }
     
        public override int GetHashCode()
        {
            return I;
        }
     
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public int I { get; set; }
    }
     
    public interface IR
    {
        bool Get(Bi bi);
    }
     
    public class G
    {
        private readonly IR _r;
     
        public G(IR r)
        {
            _r = r;
        }
     
        public bool M()
        {
            var bi = new Bi {I = 1};
            return _r.Get(bi);
        }
    }
     
    [TestFixture]
    public class Test
    {
        [Test]
        public void Test1()
        {
            var bi = new Bi {I = 1};
            var r = Mock.Create<IR>();
     
            Mock.Arrange(() => r.Get(bi)).Returns(true);
            var g = new G(r);
            var b = g.M();
            Assert.That(b, Is.True);
        }
     
    }
    No matter what I do, I can't seem to get  the BI parameter to match correctly.
    I've tried Equals and a bunch of the interfaces for testing equality and none seem to get called.
    Do I need to use the Arg.Matches or should this work and I'm just missing something?
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 12 Feb 2013 Link to this post

    Hello Eric,

    In order to implement equals functionality to your class, I did the following:
    public class Bi : IEquatable<Bi>
    {
        public bool Equals(Bi other)
        {
            return this.I.Equals(other.I);
        }
     
        public override int GetHashCode()
        {
            return I;
        }
     
        public override bool Equals(object obj)
        {
            return this.Equals(obj as Bi);
        }
     
        public int I { get; set; }
    }
     
    Having the above, now I am able to use matcher in my test case:
    [Test]
    public void Test1()
    {
        var bi = new Bi { I = 1 };
        var r = Mock.Create<IR>();
     
        Mock.Arrange(() => r.Get(Arg.Matches<Bi>(any => any.Equals(bi)))).Returns(true);
         
        var g = new G(r);
        var b = g.M();
     
        Assert.That(b, Is.True);
    }
    Note that, the test is now passing.

    I hope this helps. Please do not hesitate to contact us further if you have any questions.

    Greetings,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Russell
    Russell avatar
    76 posts
    Member since:
    Sep 2006

    Posted 12 Feb 2013 Link to this post

    I was trying to avoid using the Arg.Matches syntax in my test.  Is there anyway to do that?

    Thanks,
    Eric
  5. Rob
    Rob avatar
    1 posts
    Member since:
    Oct 2012

    Posted 12 Feb 2013 Link to this post

    I also found this behaviour surprising - I expected JustMock to compare the expected and actual arguments using the Equals method, but instead it seems to be using the == operator. Using Arg.Matches works but leads to less elegant code and shouldn't be necessary IMO.
  6. Mihail
    Admin
    Mihail avatar
    75 posts

    Posted 14 Feb 2013 Link to this post

    Hello,

    At present JustMock uses Object.ReferenceEquals(...) for matching the mock parameters. It is a nice idea to use Object.Equals(...) for matching mock parameters as well. Before we implement it we have to consider it carefully so we don't break existing the unit tests.

    I created a feature request where you can vote it.

    http://feedback.telerik.com/Project/105/Feedback/Details/41055-match-mock-parameters-by-equals

    Please vote it so we can prioritize it accordingly.

    Kind regards,
    Mihail
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top
DevCraft R3 2016 release webinar banner