Mock<Contractor> instance = new Mock<Contractor>(); Mock<Contractor> instanceToCompareTo = new Mock<Contractor>();
instance.SetupProperty<int>(x => x.Id, id); instance.SetupProperty<string>(x => x.Title, "MR."); instance.SetupProperty<string>(x => x.FirstName, "Wade"); instance.SetupProperty<string>(x => x.MiddleName, "Daniel");.....instanceToCompareTo.SetupProperty<int>(x => x.Id, id); instanceToCompareTo.SetupProperty<string>(x => x.Title, "MR."); instanceToCompareTo.SetupProperty<string>(x => x.FirstName, "Wade"); instanceToCompareTo.SetupProperty<string>(x => x.MiddleName, "Daniel")
Contractor contractor = instance.Object; Contractor contractorToCompare = instanceToCompareTo.Object;
contractor.ShouldEqual(contractorToCompare);
I get the following error:
I assume you have overwritten the Equals and GetHashCode methods on the class?
Also, you can do away with all those genetic parameters, the compiler infers them
/kzu from mobile
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
Unfortunately, that was a fail.
I created an interface and updated my code to something like this:
var mockInstance = new Mock<IGroup>(); mockInstance.Setup(x => x.Id).Returns(523); mockInstance.SetupGet(x => x.Name).Returns("BlahName1"); mockInstance.SetupGet(x => x.Description).Returns("This group is blah"); mockInstance.SetupGet(x => x.ApplicationName).Returns(string.Empty); mockInstance.SetupGet(x => x.IsApplicationAdmin).Returns("Y"); var mockInstanceToCompareTo = new Mock<IGroup>(); mockInstanceToCompareTo.SetupGet(x => x.Id).Returns(523); mockInstanceToCompareTo.SetupGet(x => x.Name).Returns("BlahName1");
mockInstanceToCompareTo.SetupGet(x => x.Description).Returns("This group is blah");
mockInstanceToCompareTo.SetupGet(x => x.ApplicationName).Returns(string.Empty); mockInstanceToCompareTo.SetupGet(x => x.IsApplicationAdmin).Returns("Y"); IGroup instance = mockInstance.Object; IGroup instanceToCompareTo = mockInstanceToCompareTo.Object;instance.ShouldEqual(instanceToCompareTo);
The plus side is that I'm using an interace which is more correct than mocking a concrete instance. The Moq code is more appropriate to what I'm trying to do also.
I think that the problem lies with the fact that Moq is using Castle.DynamicProxy and how proxies work. Please see this link:
http://stackoverflow.com/questions/2969274/castle-dynamicproxy-how-to-proxy-equals-when-proxying-an-interface
Apparently, the solution, in theory and if I'm understanding this correctly, is to create an interceptor that would call the equals method, but I'm not sure how that would be implemented at this point. I'm also not sure whether I should just check the equality of each of the properties and call it a day. I don't feel like that is the correct way to go, however.
- Robert