Giter Club home page Giter Club logo

Comments (4)

jwChung avatar jwChung commented on July 29, 2024

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;
    }
}

from moq4.

duongthaiha avatar duongthaiha commented on July 29, 2024

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);
    }
}

from moq4.

jwChung avatar jwChung commented on July 29, 2024

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;
    }
}

from moq4.

duongthaiha avatar duongthaiha commented on July 29, 2024

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

from moq4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.