Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override previous setup #89

Closed
duongthaiha opened this issue Feb 4, 2014 · 4 comments
Closed

Override previous setup #89

duongthaiha opened this issue Feb 4, 2014 · 4 comments

Comments

@duongthaiha
Copy link

Hi I am trying to set an mock of a class's method with two possible input. When i check output only the last set up return expected output. The first one did not. Any help is much appreciated.

[Test]
public void ClimbOnceTwoNeighbour_Sample()
{
 stateConverter = new Mock<StateConverter>();

solution = new Mock<Solution>();
state = new Mock<State>();

var neightbourSolution1 = new Mock<Solution>();
var neighbourState1 = new Mock<State>();
var neightbourSolution2 = new Mock<Solution>();
var neighbourState2 = new Mock<State>();

stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>())).Returns(neighbourState1.Object);
stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>())).Returns(neighbourState2.Object);

var state1 = stateConverter.Object.FromSolution(neightbourSolution1.Object, state.Object);//return null ????
var state2 = stateConverter.Object.FromSolution(neightbourSolution2.Object, state.Object);//return neighbourState2.Object)


Assert.AreEqual(neighbourState2.Object, state2);//pass test here
Assert.AreEqual(neighbourState1.Object, state1);//fail here due to null is returned from previous statement

}
@jwChung
Copy link

jwChung commented Feb 5, 2014

I cannot encounter any problem with your code. Check out my test code and compare with yours, please. I used v4.2.1312.1622 of Moq.

using Moq;
using NUnit.Framework;

[TestFixture]
class TestClass
{
    [Test]
    public void TestMethod()
    {
        var stateConverter = new Mock<StateConverter>();

        var state = new Mock<State>();

        var neightbourSolution1 = new Mock<Solution>();
        var neighbourState1 = new Mock<State>();
        var neightbourSolution2 = new Mock<Solution>();
        var neighbourState2 = new Mock<State>();

        stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>()))
            .Returns(neighbourState1.Object);
        stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>()))
            .Returns(neighbourState2.Object);

        var state1 = stateConverter.Object.FromSolution(neightbourSolution1.Object, state.Object);//return null ????
        var state2 = stateConverter.Object.FromSolution(neightbourSolution2.Object, state.Object);//return neighbourState2.Object)

        Assert.AreEqual(neighbourState2.Object, state2);//pass test here
        Assert.AreEqual(neighbourState1.Object, state1);//fail here due to null is returned from previous statement
    }
}

public class State
{
}

public class Solution
{
}

public class StateConverter
{
    public virtual object FromSolution(Solution p0, State isAny)
    {
        return null;
    }
}

@duongthaiha
Copy link
Author

Hi Thank you very much for your help. I think I found the cause for this issue. If solution class has customer equality implementation then this happen. If you change the solution class to the following then the issue can be reproduce. However I still dont know how to overcome this problem. Do I need to mock equality member as well? or is there a better way? Thank you very much in advance

public class Solution
{
    private String[] _strings;

    protected bool Equals(Solution other)
    {
        return Equals(_strings, other._strings);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((Solution) obj);
    }

    public override int GetHashCode()
    {
        return (_strings != null ? _strings.GetHashCode() : 0);
    }
}

@jwChung
Copy link

jwChung commented Feb 6, 2014

Do I need to mock equality member as well?

No, you don't. On the contrary, you need to use the Equal method in your test by setting DefaultValue as true. This would be more appropriate for your test purpose. You would be able to catch some solution from the under code.

using Moq;
using NUnit.Framework;

[TestFixture]
class TestClass
{
    [Test]
    public void TestMethod()
    {
        var stateConverter = new Mock<StateConverter>();

        var state = new Mock<State>();

        var neightbourSolution1 = new Mock<Solution>(new object[] { new[] { "foo", "bar" } }) { CallBase = true };
        var neighbourState1 = new Mock<State>();
        var neightbourSolution2 = new Mock<Solution>(new object[] { new[] { "foo", "bar" } }) { CallBase = true };
        var neighbourState2 = new Mock<State>();

        stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>()))
            .Returns(neighbourState1.Object);
        stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>()))
            .Returns(neighbourState2.Object);

        var state1 = stateConverter.Object.FromSolution(neightbourSolution1.Object, state.Object);//return null ????
        var state2 = stateConverter.Object.FromSolution(neightbourSolution2.Object, state.Object);//return neighbourState2.Object)

        Assert.AreEqual(neighbourState2.Object, state2);//pass test here
        Assert.AreEqual(neighbourState1.Object, state1);//fail here due to null is returned from previous statement
    }
}

public class State
{
}

public class Solution
{
    private string[] _strings;

    public Solution(string[] strings)
    {
        _strings = strings;
    }

    protected bool Equals(Solution other)
    {
        return Equals(_strings, other._strings);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((Solution)obj);
    }

    public override int GetHashCode()
    {
        return (_strings != null ? _strings.GetHashCode() : 0);
    }
}

public class StateConverter
{
    public virtual object FromSolution(Solution p0, State isAny)
    {
        return null;
    }
}

@duongthaiha
Copy link
Author

Thank you very much it is working. Still have to make sure to have the string [] . :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants