Moq, Mocking, and Object Equality

919 views
Skip to first unread message

Robert Eberhart

unread,
Oct 14, 2011, 4:20:20 PM10/14/11
to moq...@googlegroups.com
I'm relatively new to Moq and Mocking.  I'm using S#arp Architecture and working through the Unit Tests.  I'm using Moq to mock some objects because I have the access modifier of the setters of two of my entities set to protected.  The reason is that this is an application that uses tables from which I want to get information, but I do not want the application to add, update, or delete any of the data.  

The problem is that when I have two objects that I want to compare like this: 

     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:

Tests.Project.Core.ContractorTests.CanCompareContractors:
  Expected: <Castle.Proxies.ContractorProxy>
  But was:  <Castle.Proxies.ContractorProxy>

I'm trying to figure out if this is by design or whether I'm doing something wrong.

Thanks for any help you can offer.

- Robert

Daniel Cazzulino

unread,
Oct 14, 2011, 7:01:06 PM10/14/11
to moq...@googlegroups.com

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

Robert Eberhart

unread,
Oct 15, 2011, 10:31:26 AM10/15/11
to moq...@googlegroups.com
Excellent point about the Equals and GetHashCode methods.  I need to ensure that the entities inherit from the Entity class in S#arp Architecture and it would also probably be best to create some interfaces instead of using the concrete implementation.

Thanks for giving me some ideas.

- Robert

Daniel Cazzulino

unread,
Oct 15, 2011, 11:39:15 AM10/15/11
to moq...@googlegroups.com

Np!

/kzu from mobile

Robert Eberhart

unread,
Oct 15, 2011, 3:21:46 PM10/15/11
to moq...@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
Reply all
Reply to author
Forward
0 new messages