Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Moq can you verify a method call with an anonymous type?

Tags:

I'm trying to verify a method call using Moq, but I can't quite get the syntax right. Currently, I've got this as my verify:

repository.Verify(x => x.ExecuteNonQuery("fav_AddFavorites", new {     fid = 123,     inputStr = "000456" }), Times.Once()); 

The code compiles, but the test fails with the error:

Expected invocation on the mock once, but was 0 times:  x => x.ExecuteNonQuery("fav_AddFavorites", new <>f__AnonymousType0<Int32, String>(123, "000456")) No setups configured.  Performed invocations: IRepository.ExecuteNonQuery("fav_AddFavorites", { fid = 123, inputStr = 000456 }) 

How can I verify the method call and match the method parameters for an anonymous type?

UPDATE

To answer the questions:

I am trying to verify both that the method was called and that the parameters are correct.

The signature of the method I'm trying to verify is:

int ExecuteNonQuery(string query, object param = null); 

The setup code is simply:

repository = new Mock<IRepository>(); 

UPDATE 2

It looks like this is a problem with Moq and how it handles anonymous types in .Net. The code posted by Paul Matovich runs fine, however, once the code and the test are in different assemblies the test fails.

like image 513
ilivewithian Avatar asked Mar 27 '12 15:03

ilivewithian


People also ask

How can you check if a method was called using a mock?

To check if a method was called on a mocked object you can use the Mockito. verify method: Mockito. verify(someMock).

What does MOQ verify do?

Verifies that all verifiable expectations have been met.


1 Answers

This Passes

        public class Class1     {         private Class2 _Class2;         public Class1(Class2 class2)         {             _Class2 = class2;         }          public void DoSomething(string s)         {             _Class2.ExecuteNonQuery(s, new { fid = 123,  inputStr = "000456" });         }     }      public class Class2     {         public virtual void ExecuteNonQuery(string s, object o)         {         }     }      /// <summary>     ///A test for ExecuteNonQuery     ///</summary>     [TestMethod()]     public void ExecuteNonQueryTest()     {         string testString = "Hello";         var Class2Stub = new Mock<Class2>();         Class1 target = new Class1(Class2Stub.Object);         target.DoSomething(testString);         Class2Stub.Verify(x => x.ExecuteNonQuery(testString, It.Is<object>(o => o.Equals(new { fid = 123, inputStr = "000456" }))), Times.Once());     } 

##Update##

That is strange, it doesn't work in different assemblies. Someone can give us the long definition about why the object.equals from different assemblies behaves differently, but for different assemblies, this will work, any variance in the object values will return a different hash code.

        Class2Stub.Verify(x => x.ExecuteNonQuery(testString, It.Is<object>(o => o.GetHashCode() == (new { fid = 123, inputStr = "000456" }).GetHashCode())), Times.Once()); 
like image 109
Paul Matovich Avatar answered Oct 23 '22 20:10

Paul Matovich