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)
- Stop using Moq as a guinea pig to get feedback on and develop SponsorLink HOT 105
- Support Static Abstract property HOT 1
- Community Involvement HOT 5
- Change log is not updated since version 4.18.4 HOT 3
- Mocking Blobclient.DownloadContentAsync() HOT 3
- Poll on SponsorLink HOT 1
- Should allow null references as constructor parameters. HOT 1
- Replacement for Moq.MethodCallReturns'2 HOT 3
- The public API is still missing nullable reference type annotations HOT 1
- a minor changes to improve the readability HOT 2
- Mock verification slow with It.Is(Expression) HOT 5
- [Feature Request] Make it possible to moq concrete classes HOT 1
- Moq dependency uprev is not working as expected HOT 1
- SponsorLink Integration Makes Moq Unuasable due to PII and GDPR Requirements HOT 9
- Add the ability to compare ref argument by value HOT 1
- Cannot cast inside the Action<T> parameter of Verify method
- Wrong Setup by using String interpolation within It.Is<string> HOT 4
- Question: would it make sense to call verify all on a strict mock when the mock gets disposed?
- `DefaultValueProvider` extensibility
- Some source code files is filled with comments like: "Unmerged change from project "
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.