Comments (4)
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.
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.
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.
Thank you very much it is working. Still have to make sure to have the string [] . :-)
from moq4.
Related Issues (20)
- Moq library throwing TypeIniatilizationException. HOT 1
- Request: Ability to setup a property of type Task<ConcreteType> by mocking interface
- Supporting .NET8 HOT 2
- Mock.Protected().Verify fails sometimes
- Moq "set" matcher fails when setting StringValues with string HOT 1
- It.IsAny<StructObject>() isn't
- Documentation Unavailable Online HOT 2
- Consider adding IgnoresAccessChecksTo("Unsupported")
- Explicit support for matching cancellation tokens with It class. (It.IsCancellationToken())
- Moq Setup does not work with "object" as generic type? HOT 1
- IVerifies.Verifiable* documentation comments errata?
- Support for post-invocation callbacks on void methods?
- Semantic error in unit test MatchesIsNotInEnumerable()?
- Documentation link is broken HOT 9
- Constructing Mock<DerivedControl>.Object throws TypeLoadException HOT 2
- How do you Verify an IAsyncEnumerable<T> ?
- Add Way to Exclude Calls from VerifyNoOtherCalls HOT 1
- mock.Protected().Verify() does not work correctly HOT 2
- Constructor arguments should be nullable
- Documentation link gives 404 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moq4.