-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
Comments
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;
}
} |
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
|
No, you don't. On the contrary, you need to use the Equal method in your test by setting 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;
}
} |
Thank you very much it is working. Still have to make sure to have the string [] . :-) |
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.
The text was updated successfully, but these errors were encountered: