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

Parameter matching

4 Answers 54 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Software
Top achievements
Rank 1
Software asked on 09 Feb 2013, 10:22 PM
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?

4 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 12 Feb 2013, 04:02 PM
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.
0
Software
Top achievements
Rank 1
answered on 12 Feb 2013, 04:19 PM
I was trying to avoid using the Arg.Matches syntax in my test.  Is there anyway to do that?

Thanks,
Eric
0
Rob
Top achievements
Rank 1
answered on 12 Feb 2013, 06:33 PM
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.
0
Mihail
Telerik team
answered on 14 Feb 2013, 09:55 AM
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.
Tags
General Discussions
Asked by
Software
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Software
Top achievements
Rank 1
Rob
Top achievements
Rank 1
Mihail
Telerik team
Share this question
or