You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using Moq;using NUnit.Framework;using System;using System.Collections.Generic;namespaceIssue146Repro{/* Issue: * When a mocked method that takes a Dictionary argument is invoked a second time, the values that * the dictionary contained in the first invocation are overwritten with the values from the 2nd invocation. */[TestFixture]publicclassIssue146ReproTests{staticMock<ISimpleInterface>mockedService;[SetUp]publicvoidSetupMock(){mockedService=newMock<ISimpleInterface>();
mockedService.Setup(s => s.MethodWithDictionaryArg(It.IsAny<Dictionary<string,string>>()));}[TestCase]publicvoidTestIssue146(){SimpleClasssimpleClass=new SimpleClass(mockedService.Object);Dictionary<string,string>map=newDictionary<string,string>(){{"FirstKey","ValueForFirstKey"}};
simpleClass.DoSomething(map);// Take a look at the InterceptionContext.ActualInvocations on mockedService. At this point, the arguments to the first invocation // are just {[FirstKey, ValueForFirstKey]}. That causes this call to Verify to pass.
mockedService.Verify(s => s.MethodWithDictionaryArg( It.Is<Dictionary<string,string>>(d => d.ContainsKey("SecondKey")&& d["SecondKey"]=="ValueForSecondKey")), Times.Never());
map.Add("SecondKey","ValueForSecondKey");
simpleClass.DoSomething(map);// Take another look at the InterceptionContext.ActualInvocations. We now have two invocations (as expected), but the arguments // to the first invocation now include {[SecondKey, ValueForSecondKey]} as well as {[FirstKey, ValueForFirstKey]}.// That causes this call to Verify to fail with "Expected invocation on the mock once, but was 2 times"
mockedService.Verify(s => s.MethodWithDictionaryArg( It.Is<Dictionary<string,string>>(d => d.ContainsKey("SecondKey")&& d["SecondKey"]=="ValueForSecondKey")), Times.Once());}publicclassSimpleClass{ISimpleInterfacesimpleInterface;publicSimpleClass(ISimpleInterfaceinterfaceArg){this.simpleInterface =interfaceArg;}publicvoidDoSomething(Dictionary<string,string>map){this.simpleInterface.MethodWithDictionaryArg(map);}publicvoidMethodWithDictionaryArg(Dictionary<string,string>map){
Console.WriteLine();}}publicinterfaceISimpleInterface{voidMethodWithDictionaryArg(Dictionary<string,string>map);}}}
The text was updated successfully, but these errors were encountered:
It seems to be tied to the same instance of the dictionary being used for the two invocations. If I switch the 2nd invocation to use a new dictionary, the test passes:
You are calling a method twice with the same dictionary instance. Moq is
not changing anything. Your code is adding another key to the same
dictionary. That's just how objects work.
On Tuesday, December 16, 2014, Eric Dettinger [email protected]
wrote:
It seems to be tied to the same instance of the dictionary being used for
the two invocations. If I switch the 2nd invocation to use a new
dictionary, the test passes:
The text was updated successfully, but these errors were encountered: