+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters, p => p.StartsWith("W"))));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var capturedValue = string.Empty;
+ var match = new CaptureMatch{string}(x => capturedValue = x);
+ mock.Setup(x => x.DoSomething(Capture.With(match)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", capturedValue);
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true);
+
+
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback((string command) => Console.WriteLine(command));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true)
+ .Returns(true);
+
+ Note that in the case of value-returning methods, after the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback(command => Console.WriteLine(command))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ var mock = new Mock<IContainer>();
+
+ mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
+ .Raises(add => add.Added += null, EventArgs.Empty);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the method
+ is executed and the value the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Returns((string command) => returnValues[command]);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2) => arg1 + arg2);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3) => arg1 + arg2 + arg3);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4) => arg1 + arg2 + arg3 + arg4);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5) => arg1 + arg2 + arg3 + arg4 + arg5);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16);
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMostOnce();
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMost( 5 );
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the property
+ is retrieved and the value the
+ mock.SetupGet(x => x.Suspended)
+ .Callback(() => called = true)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended)
+ .Callback((bool state) => Console.WriteLine(state));
+
+
+ mock.Setup(x => x.Execute(""))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.Execute(""))
+ .Throws<ArgumentException>();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable("Ping should be executed always!");
+
+
+ // Typed instance, not the mock, is retrieved from some test API.
+ HttpContextBase context = GetMockContext();
+
+ // context.Request is the typed object from the "real" API
+ // so in order to add a setup to it, we need to get
+ // the mock that "owns" it
+ Mock<HttpRequestBase> request = Mock.Get(context.Request);
+ mock.Setup(req => req.AppRelativeCurrentExecutionFilePath)
+ .Returns(tempUrl);
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+ // add IDisposable interface
+ var disposable = mock.As<IDisposable>();
+ disposable.Setup(d => d.Dispose()).Verifiable();
+
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ repository.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the repository
+ to create loose mocks and later verify only verifiable setups:
+
+ var repository = new MockRepository(MockBehavior.Loose);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // this setup will be verified when we verify the repository
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the repository
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ repository.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the repository with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = repository.Create<IFoo>(MockBehavior.Loose);
+ var bar = repository.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ repository.Verify();
+
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ Note that the return value from the compiler matcher is irrelevant.
+ This method will never be called, and is just used to satisfy the
+ compiler and to signal Moq that this is not a method that we want
+ to be invoked at runtime.
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+
+ At runtime, the mocked method will be invoked with a specific
+ list of orders. This value will be passed to this runtime
+ matcher as the first argument, while the second argument is the
+ one specified in the setup (
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+ }
+
+ And the concrete test using this matcher:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ // use mock, invoke Save, and have the matcher filter.
+
+
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ mock.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.True(order.IsFilled);
+
+ The following example shows how to use the
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ // shows how to expect a value within a range
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+ // shows how to throw for unexpected calls.
+ mock.Setup(x => x.Remove(
+ It.IsAny<string>(),
+ It.IsAny<int>()))
+ .Throws(new InvalidOperationException());
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.False(order.IsFilled);
+
+ var mock = new Mock<IFormatProvider>();
+ var mock = new Mock<MyProvider>(someArgument, 25);
+ var mock = new Mock<IFormatProvider>(MockBehavior.Relaxed);
+ var mock = new Mock<MyProvider>(someArgument, 25);
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+
+ mock.Setup(x => x.HasInventory("Talisker", 50)).Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.Stub(v => v.Value);
+
+ After the
+ IHaveValue v = mock.Object;
+
+ v.Value = 5;
+ Assert.Equal(5, v.Value);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.SetupProperty(v => v.Value, 5);
+
+ After the
+ IHaveValue v = mock.Object;
+ // Initial value was stored
+ Assert.Equal(5, v.Value);
+
+ // New value set which changes the initial value
+ v.Value = 6;
+ Assert.Equal(6, v.Value);
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50), "When filling orders, inventory has to be checked");
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true, "Warehouse should always be closed after the action");
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));
+
+
+ var mockView = new Mock<IOrdersView>();
+ var presenter = new OrdersPresenter(mockView.Object);
+
+ // Check that the presenter has no selection by default
+ Assert.Null(presenter.SelectedOrder);
+
+ // Raise the event with a specific arguments data
+ mockView.Raise(v => v.SelectionChanged += null, new OrderEventArgs { Order = new Order("moq", 500) });
+
+ // Now the presenter reacted to the event, and we have a selected order
+ Assert.NotNull(presenter.SelectedOrder);
+ Assert.Equal("moq", presenter.SelectedOrder.ProductName);
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.MyEvent -= null, "Name", bool, 25);
+
+
+ mock.SetupSet(x => x.Suspended);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ factory.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the factory
+ to create loose mocks and later verify only verifiable setups:
+
+ var factory = new MockFactory(MockBehavior.Loose);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // this setup will be verified when we verify the factory
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the factory
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ factory.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the factory with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+ var bar = factory.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ // use mock on tests
+
+ factory.VerifyAll();
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>("Foo", 25, true);
+ // use mock on tests
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>(MockBehavior.Strict, "Foo", 25, true);
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());
+
+
+ mock.Setup(x => x.Do(It.Is<int>(i => i % 2 == 0)))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Setup(x => x.GetUser(It.Is<int>(i => i < 0)))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(1, 2, 3)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(1, 2, 3)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+"))).Returns(1);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase))).Returns(1);
+
+
+ // Throws an exception for a call to Remove with a null string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsNull<string>())
+ .Throws(new InvalidOperationException());
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsAny<string>())
+ .Throws(new InvalidOperationException());
+
+
+ mock.Protected()
+ .Setup("Do", ItExpr.Is<int>(i => i % 2 == 0))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Protected()
+ .Setup("GetUser", ItExpr.Is<int>(i => i < 0))
+ .Throws(new ArgumentException());
+
+
+ mock.Protected()
+ .Setup("HasInventory",
+ ItExpr.IsAny<string>(),
+ ItExpr.IsInRange(0, 100, Range.Inclusive))
+ .Returns(false);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+"))
+ .Returns(1);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+", RegexOptions.IgnoreCase))
+ .Returns(1);
+
+
+ [Matcher]
+ public Order IsBigOrder()
+ {
+ return Match<Order>.Create(
+ o => o.GrandTotal >= 5000,
+ /* a friendly expression to render on failures */
+ () => IsBigOrder());
+ }
+
+ This method can be used in any mock setup invocation:
+
+ mock.Setup(m => m.Submit(IsBigOrder()).Throws<UnauthorizedAccessException>();
+
+ At runtime, Moq knows that the return value was a matcher (note that the method MUST be
+ annotated with the [Matcher] attribute in order to determine this) and
+ evaluates your predicate with the actual value passed into your predicate.
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters, p => p.StartsWith("W"))));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var capturedValue = string.Empty;
+ var match = new CaptureMatch{string}(x => capturedValue = x);
+ mock.Setup(x => x.DoSomething(Capture.With(match)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", capturedValue);
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());
+
+
+ mock.Setup(x => x.Do(It.Is<int>(i => i % 2 == 0)))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Setup(x => x.GetUser(It.Is<int>(i => i < 0)))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(1, 2, 3)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(1, 2, 3)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+"))).Returns(1);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase))).Returns(1);
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true);
+
+
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback((string command) => Console.WriteLine(command));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true)
+ .Returns(true);
+
+ Note that in the case of value-returning methods, after the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback(command => Console.WriteLine(command))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Callback(() => called = true)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended)
+ .Callback((bool state) => Console.WriteLine(state));
+
+
+ var mock = new Mock<IContainer>();
+
+ mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
+ .Raises(add => add.Added += null, EventArgs.Empty);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the method
+ is executed and the value the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Returns((string command) => returnValues[command]);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2) => arg1 + arg2);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3) => arg1 + arg2 + arg3);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4) => arg1 + arg2 + arg3 + arg4);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5) => arg1 + arg2 + arg3 + arg4 + arg5);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the property
+ is retrieved and the value the
+ mock.Setup(x => x.Execute(""))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.Execute(""))
+ .Throws<ArgumentException>();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable("Ping should be executed always!");
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMostOnce();
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMost( 5 );
+
+
+ // Typed instance, not the mock, is retrieved from some test API.
+ HttpContextBase context = GetMockContext();
+
+ // context.Request is the typed object from the "real" API
+ // so in order to add a setup to it, we need to get
+ // the mock that "owns" it
+ Mock<HttpRequestBase> request = Mock.Get(context.Request);
+ mock.Setup(req => req.AppRelativeCurrentExecutionFilePath)
+ .Returns(tempUrl);
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+ // add IDisposable interface
+ var disposable = mock.As<IDisposable>();
+ disposable.Setup(d => d.Dispose()).Verifiable();
+
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ repository.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the repository
+ to create loose mocks and later verify only verifiable setups:
+
+ var repository = new MockRepository(MockBehavior.Loose);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // this setup will be verified when we verify the repository
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the repository
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ repository.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the repository with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = repository.Create<IFoo>(MockBehavior.Loose);
+ var bar = repository.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ repository.Verify();
+
+
+ [Matcher]
+ public Order IsBigOrder()
+ {
+ return Match<Order>.Create(
+ o => o.GrandTotal >= 5000,
+ /* a friendly expression to render on failures */
+ () => IsBigOrder());
+ }
+
+ This method can be used in any mock setup invocation:
+
+ mock.Setup(m => m.Submit(IsBigOrder()).Throws<UnauthorizedAccessException>();
+
+ At runtime, Moq knows that the return value was a matcher (note that the method MUST be
+ annotated with the [Matcher] attribute in order to determine this) and
+ evaluates your predicate with the actual value passed into your predicate.
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ Note that the return value from the compiler matcher is irrelevant.
+ This method will never be called, and is just used to satisfy the
+ compiler and to signal Moq that this is not a method that we want
+ to be invoked at runtime.
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+
+ At runtime, the mocked method will be invoked with a specific
+ list of orders. This value will be passed to this runtime
+ matcher as the first argument, while the second argument is the
+ one specified in the setup (
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+ }
+
+ And the concrete test using this matcher:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ // use mock, invoke Save, and have the matcher filter.
+
+
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ mock.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.True(order.IsFilled);
+
+ The following example shows how to use the
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ // shows how to expect a value within a range
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+ // shows how to throw for unexpected calls.
+ mock.Setup(x => x.Remove(
+ It.IsAny<string>(),
+ It.IsAny<int>()))
+ .Throws(new InvalidOperationException());
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.False(order.IsFilled);
+
+ var mock = new Mock<IFormatProvider>();
+ var mock = new Mock<MyProvider>(someArgument, 25);
+ var mock = new Mock<IFormatProvider>(MockBehavior.Relaxed);
+ var mock = new Mock<MyProvider>(someArgument, 25);
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+
+ mock.Setup(x => x.HasInventory("Talisker", 50)).Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.Stub(v => v.Value);
+
+ After the
+ IHaveValue v = mock.Object;
+
+ v.Value = 5;
+ Assert.Equal(5, v.Value);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.SetupProperty(v => v.Value, 5);
+
+ After the
+ IHaveValue v = mock.Object;
+ // Initial value was stored
+ Assert.Equal(5, v.Value);
+
+ // New value set which changes the initial value
+ v.Value = 6;
+ Assert.Equal(6, v.Value);
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50), "When filling orders, inventory has to be checked");
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true, "Warehouse should always be closed after the action");
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));
+
+
+ var mockView = new Mock<IOrdersView>();
+ var presenter = new OrdersPresenter(mockView.Object);
+
+ // Check that the presenter has no selection by default
+ Assert.Null(presenter.SelectedOrder);
+
+ // Raise the event with a specific arguments data
+ mockView.Raise(v => v.SelectionChanged += null, new OrderEventArgs { Order = new Order("moq", 500) });
+
+ // Now the presenter reacted to the event, and we have a selected order
+ Assert.NotNull(presenter.SelectedOrder);
+ Assert.Equal("moq", presenter.SelectedOrder.ProductName);
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.MyEvent -= null, "Name", bool, 25);
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ factory.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the factory
+ to create loose mocks and later verify only verifiable setups:
+
+ var factory = new MockFactory(MockBehavior.Loose);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // this setup will be verified when we verify the factory
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the factory
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ factory.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the factory with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+ var bar = factory.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ // use mock on tests
+
+ factory.VerifyAll();
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>("Foo", 25, true);
+ // use mock on tests
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>(MockBehavior.Strict, "Foo", 25, true);
+
+
+ mock.SetupSet(x => x.Suspended);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ // Throws an exception for a call to Remove with a null string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsNull<string>())
+ .Throws(new InvalidOperationException());
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsAny<string>())
+ .Throws(new InvalidOperationException());
+
+
+ mock.Protected()
+ .Setup("Do", ItExpr.Is<int>(i => i % 2 == 0))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Protected()
+ .Setup("GetUser", ItExpr.Is<int>(i => i < 0))
+ .Throws(new ArgumentException());
+
+
+ mock.Protected()
+ .Setup("HasInventory",
+ ItExpr.IsAny<string>(),
+ ItExpr.IsInRange(0, 100, Range.Inclusive))
+ .Returns(false);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+"))
+ .Returns(1);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+", RegexOptions.IgnoreCase))
+ .Returns(1);
+
+ Math.Max( , ).
+ When setting this property, Math.Min( , ).
+ The caret is not scrolled into view.
+ Math.Min( , ).
+ When setting this property, Math.Max( , ).
+ The caret is not scrolled into view.
+
+ public class MyNotificationsTest : NotifyPropertyBase
+ {
+ private int myInt = 0;
+ private int myInt2 = 0; //
+
+ public int AsInt
+ {
+ get
+ {
+ return this.myField;
+ }
+ set
+ {
+ if (SetProperty("AsInt", ref this.myInt, value))
+ {
+ // perform additional actions when new value is set to myInt.
+ }
+ }
+ }
+
+ public int AsInt2
+ {
+ get
+ {
+ return (float)this.myInt2;
+ }
+ set
+ {
+ // The following property setter is the same as the previous one.
+ if (this.myInt2 != value)
+ {
+ PropertyChangingEventArgs2 ea = new PropertyChangingEventArgs2("AsInt2", value);
+ OnPropertyChanging(ea);
+
+ if (!ea.Cancel)
+ {
+ this.myInt2 = (int)ea.Value;
+ OnPropertyChanged("AsInt2");
+
+ // perform additional actions when new value is set to myInt2.
+ }
+ }
+ }
+ }
+ }
+
+
+ Country, City, count(Country) Items, ContactName Group By Country, City desc
+
+
+ Country, City, count(Country) Items, ContactName Group By Country, City desc
+
+
+ Dim groupExpression As GridGroupByExpression = New GridGroupByExpression()
+
+ Dim groupByField As GridGroupByField = New GridGroupByField()
+
+ groupByField = New GridGroupByField()
+ groupByField.FieldName = "Received"
+ groupExpression.SelectFields.Add(groupByField)
+
+ groupByField = New GridGroupByField()
+ groupByField.FieldName = "Received"
+ groupExpression.GroupByFields.Add(groupByField)
+
+ RadGrid1.MasterTableView.GroupByExpressions.Add(groupExpression)
+
+
+ GridGroupByExpression groupExpression = new GridGroupByExpression();
+
+ GridGroupByField groupByField = new GridGroupByField();
+ groupByField = new GridGroupByField();
+ groupByField.FieldName = "Received";
+ groupExpression.SelectFields.Add(groupByField);
+
+ groupByField = new GridGroupByField();
+ groupByField.FieldName = "Received";
+ groupExpression.GroupByFields.Add(groupByField);
+
+ RadGrid1.MasterTableView.GroupByExpressions.Add(groupExpression);
+
+
+ Dim groupExpression As GridGroupByExpression = New GridGroupByExpression()
+
+ Dim groupByField As GridGroupByField = New GridGroupByField()
+ groupByField.FieldName = "Size"
+ groupByField.SetAggregate(GridAggregateFunction.Sum)
+ groupExpression.SelectFields.Add(groupByField)
+
+ groupByField = New GridGroupByField()
+ groupByField.FieldName = "Received"
+ groupExpression.SelectFields.Add(groupByField)
+
+ groupByField = New GridGroupByField()
+ groupByField.FieldName = "Received"
+ groupExpression.GroupByFields.Add(groupByField)
+
+ RadGrid1.MasterTableView.GroupByExpressions.Add(groupExpression)
+
+
+ GridGroupByExpression groupExpression = new GridGroupByExpression();
+
+ GridGroupByField groupByField = new GridGroupByField();
+ groupByField.FieldName = "Size";
+ groupByField.SetAggregate(GridAggregateFunction.Sum);
+ groupExpression.SelectFields.Add(groupByField);
+
+ groupByField = new GridGroupByField();
+ groupByField.FieldName = "Received";
+ groupExpression.SelectFields.Add(groupByField);
+
+ groupByField = new GridGroupByField();
+ groupByField.FieldName = "Received";
+ groupExpression.GroupByFields.Add(groupByField);
+
+ RadGrid1.MasterTableView.GroupByExpressions.Add(groupExpression);
+
+
+ GridGroupByExpression groupExpression = new GridGroupByExpression();
+
+ groupByField = new GridGroupByField();
+ groupByField.FieldName = "Received";
+ groupExpression.SelectFields.Add(groupByField);
+
+ groupByField = new GridGroupByField();
+ groupByField.FieldName = "Received";
+ groupByField.SetSortOrder(GridSortOrder.Ascending);
+ groupExpression.GroupByFields.Add(groupByField);
+
+ RadGrid1.MasterTableView.GroupByExpressions.Add(groupExpression);
+
+
+ Dim groupExpression As GridGroupByExpression = New GridGroupByExpression()
+
+ Dim groupByField As GridGroupByField = New GridGroupByField()
+
+ groupByField = New GridGroupByField()
+ groupByField.FieldName = "Received"
+ groupExpression.SelectFields.Add(groupByField)
+
+ groupByField = New GridGroupByField()
+ groupByField.FieldName = "Received"
+ groupByField.SetSortOrder(GridSortOrder.Descending)
+ groupExpression.GroupByFields.Add(groupByField)
+
+ RadGrid1.MasterTableView.GroupByExpressions.Add(groupExpression)
+
+
+ Dim groupExpression As GridGroupByExpression = RadGrid1.MasterTableView.GroupByExpressions(0)
+ Dim headerText as String = groupExpression.SelectFields(0).GetHeaderText()
+
+
+ GridGroupByExpression groupExpression = RadGrid1.MasterTableView.GroupByExpressions[0] as GridGroupByExpression;
+ String headerText = groupExpression.SelectFields[0].GetHeaderText()
+
+
+ GridGroupByField gridGroupByField;
+
+ gridGroupByField = new GridGroupByField();
+ gridGroupByField.FieldName = "Freight";
+ gridGroupByField.HeaderText = "Total shipping cost is ";
+ gridGroupByField.Aggregate = GridAggregateFunction.Sum;
+ expression.SelectFields.Add( gridGroupByField );
+
+
+ Dim gridGroupByField As GridGroupByField
+
+ gridGroupByField = New GridGroupByField
+ gridGroupByField.FieldName = "Freight"
+ gridGroupByField.HeaderText = "Total shipping cost is "
+ gridGroupByField.Aggregate = GridAggregateFunction.Sum
+ expression.SelectFields.Add(gridGroupByField)
+
+
+ GridGroupByField gridGroupByField;
+
+ gridGroupByField = new GridGroupByField();
+ gridGroupByField.FieldName = "EmployeeID";
+ gridGroupByField.HeaderText = "EmployeeNo";
+ expression.SelectFields.Add( gridGroupByField );
+
+
+ Dim gridGroupByField As GridGroupByField
+
+ gridGroupByField = New GridGroupByField
+ gridGroupByField.FieldName = "EmployeeID"
+ gridGroupByField.HeaderText = "EmployeeNo"
+ expression.SelectFields.Add(gridGroupByField)
+
+
+ GridGroupByField gridGroupByField;
+
+ //Add select fields (before the "Group By" clause)
+ gridGroupByField = new GridGroupByField();
+ gridGroupByField.FieldName = "EmployeeID";
+ gridGroupByField.FieldAlias = "EmployeeIdentificator";
+ expression.SelectFields.Add( gridGroupByField );
+
+
+ Dim gridGroupByField As GridGroupByField
+
+ 'Add select fields (before the "Group By" clause)
+ gridGroupByField = New GridGroupByField
+ gridGroupByField.FieldName = "EmployeeID"
+ gridGroupByField.FieldAlias = "EmployeeIdentificator"
+ expression.SelectFields.Add(gridGroupByField)
+
+
+ GridGroupByField gridGroupByField;
+
+ gridGroupByField = new GridGroupByField();
+ gridGroupByField.FieldName = "EmployeeID";
+ gridGroupByField.SortOrder = GridSortOrder.Descending;
+ expression.GroupByFields.Add( gridGroupByField );
+
+
+ Dim gridGroupByField As GridGroupByField
+
+ gridGroupByField = New GridGroupByField
+ gridGroupByField.FieldName = "EmployeeID"
+ gridGroupByField.SortOrder = GridSortOrder.Descending
+ expression.GroupByFields.Add(gridGroupByField)
+
+
+ void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
+ {
+ if (e.RowElement.RowInfo.Cells.Count > 0 && (string)e.RowElement.RowInfo.Cells[0].Value == "3")
+ e.RowElement.Font = strikeoutFont;
+ else
+ e.RowElement.Font = SystemFonts.DialogFont;
+ }
+
+
+ void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
+ {
+ if (e.RowElement.RowInfo.Cells.Count > 0 && (string)e.RowElement.RowInfo.Cells[0].Value == "3")
+ e.RowElement.Font = strikeoutFont;
+ else
+ e.RowElement.Font = SystemFonts.DialogFont;
+ }
+
+ | Culture Name | +Culture Identifier | +Language-Country/Region | +
|---|---|---|
| "" (empty string) | +0x007F | +invariant culture | +
| af | +0x0036 | +Afrikaans | +
| af-ZA | +0x0436 | +Afrikaans - South Africa | +
| sq | +0x001C | +Albanian | +
| sq-AL | +0x041C | +Albanian - Albania | +
| ar | +0x0001 | +Arabic | +
| ar-DZ | +0x1401 | +Arabic - Algeria | +
| ar-BH | +0x3C01 | +Arabic - Bahrain | +
| ar-EG | +0x0C01 | +Arabic - Egypt | +
| ar-IQ | +0x0801 | +Arabic - Iraq | +
| ar-JO | +0x2C01 | +Arabic - Jordan | +
| ar-KW | +0x3401 | +Arabic - Kuwait | +
| ar-LB | +0x3001 | +Arabic - Lebanon | +
| ar-LY | +0x1001 | +Arabic - Libya | +
| ar-MA | +0x1801 | +Arabic - Morocco | +
| ar-OM | +0x2001 | +Arabic - Oman | +
| ar-QA | +0x4001 | +Arabic - Qatar | +
| ar-SA | +0x0401 | +Arabic - Saudi Arabia | +
| ar-SY | +0x2801 | +Arabic - Syria | +
| ar-TN | +0x1C01 | +Arabic - Tunisia | +
| ar-AE | +0x3801 | +Arabic - United Arab Emirates | +
| ar-YE | +0x2401 | +Arabic - Yemen | +
| hy | +0x002B | +Armenian | +
| hy-AM | +0x042B | +Armenian - Armenia | +
| az | +0x002C | +Azeri | +
| az-AZ-Cyrl | +0x082C | +Azeri (Cyrillic) - Azerbaijan | +
| az-AZ-Latn | +0x042C | +Azeri (Latin) - Azerbaijan | +
| eu | +0x002D | +Basque | +
| eu-ES | +0x042D | +Basque - Basque | +
| be | +0x0023 | +Belarusian | +
| be-BY | +0x0423 | +Belarusian - Belarus | +
| bg | +0x0002 | +Bulgarian | +
| bg-BG | +0x0402 | +Bulgarian - Bulgaria | +
| ca | +0x0003 | +Catalan | +
| ca-ES | +0x0403 | +Catalan - Catalan | +
| zh-HK | +0x0C04 | +Chinese - Hong Kong SAR | +
| zh-MO | +0x1404 | +Chinese - Macau SAR | +
| zh-CN | +0x0804 | +Chinese - China | +
| zh-CHS | +0x0004 | +Chinese (Simplified) | +
| zh-SG | +0x1004 | +Chinese - Singapore | +
| zh-TW | +0x0404 | +Chinese - Taiwan | +
| zh-CHT | +0x7C04 | +Chinese (Traditional) | +
| hr | +0x001A | +Croatian | +
| hr-HR | +0x041A | +Croatian - Croatia | +
| cs | +0x0005 | +Czech | +
| cs-CZ | +0x0405 | +Czech - Czech Republic | +
| da | +0x0006 | +Danish | +
| da-DK | +0x0406 | +Danish - Denmark | +
| div | +0x0065 | +Dhivehi | +
| div-MV | +0x0465 | +Dhivehi - Maldives | +
| nl | +0x0013 | +Dutch | +
| nl-BE | +0x0813 | +Dutch - Belgium | +
| nl-NL | +0x0413 | +Dutch - The Netherlands | +
| en | +0x0009 | +English | +
| en-AU | +0x0C09 | +English - Australia | +
| en-BZ | +0x2809 | +English - Belize | +
| en-CA | +0x1009 | +English - Canada | +
| en-CB | +0x2409 | +English - Caribbean | +
| en-IE | +0x1809 | +English - Ireland | +
| en-JM | +0x2009 | +English - Jamaica | +
| en-NZ | +0x1409 | +English - New Zealand | +
| en-PH | +0x3409 | +English - Philippines | +
| en-ZA | +0x1C09 | +English - South Africa | +
| en-TT | +0x2C09 | +English - Trinidad and Tobago | +
| en-GB | +0x0809 | +English - United Kingdom | +
| en-US | +0x0409 | +English - United States | +
| en-ZW | +0x3009 | +English - Zimbabwe | +
| et | +0x0025 | +Estonian | +
| et-EE | +0x0425 | +Estonian - Estonia | +
| fo | +0x0038 | +Faroese | +
| fo-FO | +0x0438 | +Faroese - Faroe Islands | +
| fa | +0x0029 | +Farsi | +
| fa-IR | +0x0429 | +Farsi - Iran | +
| fi | +0x000B | +Finnish | +
| fi-FI | +0x040B | +Finnish - Finland | +
| fr | +0x000C | +French | +
| fr-BE | +0x080C | +French - Belgium | +
| fr-CA | +0x0C0C | +French - Canada | +
| fr-FR | +0x040C | +French - France | +
| fr-LU | +0x140C | +French - Luxembourg | +
| fr-MC | +0x180C | +French - Monaco | +
| fr-CH | +0x100C | +French - Switzerland | +
| gl | +0x0056 | +Galician | +
| gl-ES | +0x0456 | +Galician - Galician | +
| ka | +0x0037 | +Georgian | +
| ka-GE | +0x0437 | +Georgian - Georgia | +
| de | +0x0007 | +German | +
| de-AT | +0x0C07 | +German - Austria | +
| de-DE | +0x0407 | +German - Germany | +
| de-LI | +0x1407 | +German - Liechtenstein | +
| de-LU | +0x1007 | +German - Luxembourg | +
| de-CH | +0x0807 | +German - Switzerland | +
| el | +0x0008 | +Greek | +
| el-GR | +0x0408 | +Greek - Greece | +
| gu | +0x0047 | +Gujarati | +
| gu-IN | +0x0447 | +Gujarati - India | +
| he | +0x000D | +Hebrew | +
| he-IL | +0x040D | +Hebrew - Israel | +
| hi | +0x0039 | +Hindi | +
| hi-IN | +0x0439 | +Hindi - India | +
| hu | +0x000E | +Hungarian | +
| hu-HU | +0x040E | +Hungarian - Hungary | +
| is | +0x000F | +Icelandic | +
| is-IS | +0x040F | +Icelandic - Iceland | +
| id | +0x0021 | +Indonesian | +
| id-ID | +0x0421 | +Indonesian - Indonesia | +
| it | +0x0010 | +Italian | +
| it-IT | +0x0410 | +Italian - Italy | +
| it-CH | +0x0810 | +Italian - Switzerland | +
| ja | +0x0011 | +Japanese | +
| ja-JP | +0x0411 | +Japanese - Japan | +
| kn | +0x004B | +Kannada | +
| kn-IN | +0x044B | +Kannada - India | +
| kk | +0x003F | +Kazakh | +
| kk-KZ | +0x043F | +Kazakh - Kazakhstan | +
| kok | +0x0057 | +Konkani | +
| kok-IN | +0x0457 | +Konkani - India | +
| ko | +0x0012 | +Korean | +
| ko-KR | +0x0412 | +Korean - Korea | +
| ky | +0x0040 | +Kyrgyz | +
| ky-KZ | +0x0440 | +Kyrgyz - Kazakhstan | +
| lv | +0x0026 | +Latvian | +
| lv-LV | +0x0426 | +Latvian - Latvia | +
| lt | +0x0027 | +Lithuanian | +
| lt-LT | +0x0427 | +Lithuanian - Lithuania | +
| mk | +0x002F | +Macedonian | +
| mk-MK | +0x042F | +Macedonian - FYROM | +
| ms | +0x003E | +Malay | +
| ms-BN | +0x083E | +Malay - Brunei | +
| ms-MY | +0x043E | +Malay - Malaysia | +
| mr | +0x004E | +Marathi | +
| mr-IN | +0x044E | +Marathi - India | +
| mn | +0x0050 | +Mongolian | +
| mn-MN | +0x0450 | +Mongolian - Mongolia | +
| no | +0x0014 | +Norwegian | +
| nb-NO | +0x0414 | +Norwegian (Bokmål) - Norway | +
| nn-NO | +0x0814 | +Norwegian (Nynorsk) - Norway | +
| pl | +0x0015 | +Polish | +
| pl-PL | +0x0415 | +Polish - Poland | +
| pt | +0x0016 | +Portuguese | +
| pt-BR | +0x0416 | +Portuguese - Brazil | +
| pt-PT | +0x0816 | +Portuguese - Portugal | +
| pa | +0x0046 | +Punjabi | +
| pa-IN | +0x0446 | +Punjabi - India | +
| ro | +0x0018 | +Romanian | +
| ro-RO | +0x0418 | +Romanian - Romania | +
| ru | +0x0019 | +Russian | +
| ru-RU | +0x0419 | +Russian - Russia | +
| sa | +0x004F | +Sanskrit | +
| sa-IN | +0x044F | +Sanskrit - India | +
| sr-SP-Cyrl | +0x0C1A | +Serbian (Cyrillic) - Serbia | +
| sr-SP-Latn | +0x081A | +Serbian (Latin) - Serbia | +
| sk | +0x001B | +Slovak | +
| sk-SK | +0x041B | +Slovak - Slovakia | +
| sl | +0x0024 | +Slovenian | +
| sl-SI | +0x0424 | +Slovenian - Slovenia | +
| es | +0x000A | +Spanish | +
| es-AR | +0x2C0A | +Spanish - Argentina | +
| es-BO | +0x400A | +Spanish - Bolivia | +
| es-CL | +0x340A | +Spanish - Chile | +
| es-CO | +0x240A | +Spanish - Colombia | +
| es-CR | +0x140A | +Spanish - Costa Rica | +
| es-DO | +0x1C0A | +Spanish - Dominican Republic | +
| es-EC | +0x300A | +Spanish - Ecuador | +
| es-SV | +0x440A | +Spanish - El Salvador | +
| es-GT | +0x100A | +Spanish - Guatemala | +
| es-HN | +0x480A | +Spanish - Honduras | +
| es-MX | +0x080A | +Spanish - Mexico | +
| es-NI | +0x4C0A | +Spanish - Nicaragua | +
| es-PA | +0x180A | +Spanish - Panama | +
| es-PY | +0x3C0A | +Spanish - Paraguay | +
| es-PE | +0x280A | +Spanish - Peru | +
| es-PR | +0x500A | +Spanish - Puerto Rico | +
| es-ES | +0x0C0A | +Spanish - Spain | +
| es-UY | +0x380A | +Spanish - Uruguay | +
| es-VE | +0x200A | +Spanish - Venezuela | +
| sw | +0x0041 | +Swahili | +
| sw-KE | +0x0441 | +Swahili - Kenya | +
| sv | +0x001D | +Swedish | +
| sv-FI | +0x081D | +Swedish - Finland | +
| sv-SE | +0x041D | +Swedish - Sweden | +
| syr | +0x005A | +Syriac | +
| syr-SY | +0x045A | +Syriac - Syria | +
| ta | +0x0049 | +Tamil | +
| ta-IN | +0x0449 | +Tamil - India | +
| tt | +0x0044 | +Tatar | +
| tt-RU | +0x0444 | +Tatar - Russia | +
| te | +0x004A | +Telugu | +
| te-IN | +0x044A | +Telugu - India | +
| th | +0x001E | +Thai | +
| th-TH | +0x041E | +Thai - Thailand | +
| tr | +0x001F | +Turkish | +
| tr-TR | +0x041F | +Turkish - Turkey | +
| uk | +0x0022 | +Ukrainian | +
| uk-UA | +0x0422 | +Ukrainian - Ukraine | +
| ur | +0x0020 | +Urdu | +
| ur-PK | +0x0420 | +Urdu - Pakistan | +
| uz | +0x0043 | +Uzbek | +
| uz-UZ-Cyrl | +0x0843 | +Uzbek (Cyrillic) - Uzbekistan | +
| uz-UZ-Latn | +0x0443 | +Uzbek (Latin) - Uzbekistan | +
| vi | +0x002A | +Vietnamese | +
| vi-VN | +0x042A | +Vietnamese - Vietnam | +
+ private void Page_Load(object sender, System.EventArgs e)
+ {
+ RadDateInput1.DateFormat = "M/d/yyyy"; //Short date pattern. The same as "d".
+ }
+
+
+ Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
+ RadDateInput1.DateFormat = "M/d/yyyy" 'Short date pattern. The same as "d".
+ End Sub
+
+ 


| Culture Name | +Culture Identifier | +Language-Country/Region | +
|---|---|---|
| "" (empty string) | +0x007F | +invariant culture | +
| af | +0x0036 | +Afrikaans | +
| af-ZA | +0x0436 | +Afrikaans - South Africa | +
| sq | +0x001C | +Albanian | +
| sq-AL | +0x041C | +Albanian - Albania | +
| ar | +0x0001 | +Arabic | +
| ar-DZ | +0x1401 | +Arabic - Algeria | +
| ar-BH | +0x3C01 | +Arabic - Bahrain | +
| ar-EG | +0x0C01 | +Arabic - Egypt | +
| ar-IQ | +0x0801 | +Arabic - Iraq | +
| ar-JO | +0x2C01 | +Arabic - Jordan | +
| ar-KW | +0x3401 | +Arabic - Kuwait | +
| ar-LB | +0x3001 | +Arabic - Lebanon | +
| ar-LY | +0x1001 | +Arabic - Libya | +
| ar-MA | +0x1801 | +Arabic - Morocco | +
| ar-OM | +0x2001 | +Arabic - Oman | +
| ar-QA | +0x4001 | +Arabic - Qatar | +
| ar-SA | +0x0401 | +Arabic - Saudi Arabia | +
| ar-SY | +0x2801 | +Arabic - Syria | +
| ar-TN | +0x1C01 | +Arabic - Tunisia | +
| ar-AE | +0x3801 | +Arabic - United Arab Emirates | +
| ar-YE | +0x2401 | +Arabic - Yemen | +
| hy | +0x002B | +Armenian | +
| hy-AM | +0x042B | +Armenian - Armenia | +
| az | +0x002C | +Azeri | +
| az-AZ-Cyrl | +0x082C | +Azeri (Cyrillic) - Azerbaijan | +
| az-AZ-Latn | +0x042C | +Azeri (Latin) - Azerbaijan | +
| eu | +0x002D | +Basque | +
| eu-ES | +0x042D | +Basque - Basque | +
| be | +0x0023 | +Belarusian | +
| be-BY | +0x0423 | +Belarusian - Belarus | +
| bg | +0x0002 | +Bulgarian | +
| bg-BG | +0x0402 | +Bulgarian - Bulgaria | +
| ca | +0x0003 | +Catalan | +
| ca-ES | +0x0403 | +Catalan - Catalan | +
| zh-HK | +0x0C04 | +Chinese - Hong Kong SAR | +
| zh-MO | +0x1404 | +Chinese - Macau SAR | +
| zh-CN | +0x0804 | +Chinese - China | +
| zh-CHS | +0x0004 | +Chinese (Simplified) | +
| zh-SG | +0x1004 | +Chinese - Singapore | +
| zh-TW | +0x0404 | +Chinese - Taiwan | +
| zh-CHT | +0x7C04 | +Chinese (Traditional) | +
| hr | +0x001A | +Croatian | +
| hr-HR | +0x041A | +Croatian - Croatia | +
| cs | +0x0005 | +Czech | +
| cs-CZ | +0x0405 | +Czech - Czech Republic | +
| da | +0x0006 | +Danish | +
| da-DK | +0x0406 | +Danish - Denmark | +
| div | +0x0065 | +Dhivehi | +
| div-MV | +0x0465 | +Dhivehi - Maldives | +
| nl | +0x0013 | +Dutch | +
| nl-BE | +0x0813 | +Dutch - Belgium | +
| nl-NL | +0x0413 | +Dutch - The Netherlands | +
| en | +0x0009 | +English | +
| en-AU | +0x0C09 | +English - Australia | +
| en-BZ | +0x2809 | +English - Belize | +
| en-CA | +0x1009 | +English - Canada | +
| en-CB | +0x2409 | +English - Caribbean | +
| en-IE | +0x1809 | +English - Ireland | +
| en-JM | +0x2009 | +English - Jamaica | +
| en-NZ | +0x1409 | +English - New Zealand | +
| en-PH | +0x3409 | +English - Philippines | +
| en-ZA | +0x1C09 | +English - South Africa | +
| en-TT | +0x2C09 | +English - Trinidad and Tobago | +
| en-GB | +0x0809 | +English - United Kingdom | +
| en-US | +0x0409 | +English - United States | +
| en-ZW | +0x3009 | +English - Zimbabwe | +
| et | +0x0025 | +Estonian | +
| et-EE | +0x0425 | +Estonian - Estonia | +
| fo | +0x0038 | +Faroese | +
| fo-FO | +0x0438 | +Faroese - Faroe Islands | +
| fa | +0x0029 | +Farsi | +
| fa-IR | +0x0429 | +Farsi - Iran | +
| fi | +0x000B | +Finnish | +
| fi-FI | +0x040B | +Finnish - Finland | +
| fr | +0x000C | +French | +
| fr-BE | +0x080C | +French - Belgium | +
| fr-CA | +0x0C0C | +French - Canada | +
| fr-FR | +0x040C | +French - France | +
| fr-LU | +0x140C | +French - Luxembourg | +
| fr-MC | +0x180C | +French - Monaco | +
| fr-CH | +0x100C | +French - Switzerland | +
| gl | +0x0056 | +Galician | +
| gl-ES | +0x0456 | +Galician - Galician | +
| ka | +0x0037 | +Georgian | +
| ka-GE | +0x0437 | +Georgian - Georgia | +
| de | +0x0007 | +German | +
| de-AT | +0x0C07 | +German - Austria | +
| de-DE | +0x0407 | +German - Germany | +
| de-LI | +0x1407 | +German - Liechtenstein | +
| de-LU | +0x1007 | +German - Luxembourg | +
| de-CH | +0x0807 | +German - Switzerland | +
| el | +0x0008 | +Greek | +
| el-GR | +0x0408 | +Greek - Greece | +
| gu | +0x0047 | +Gujarati | +
| gu-IN | +0x0447 | +Gujarati - India | +
| he | +0x000D | +Hebrew | +
| he-IL | +0x040D | +Hebrew - Israel | +
| hi | +0x0039 | +Hindi | +
| hi-IN | +0x0439 | +Hindi - India | +
| hu | +0x000E | +Hungarian | +
| hu-HU | +0x040E | +Hungarian - Hungary | +
| is | +0x000F | +Icelandic | +
| is-IS | +0x040F | +Icelandic - Iceland | +
| id | +0x0021 | +Indonesian | +
| id-ID | +0x0421 | +Indonesian - Indonesia | +
| it | +0x0010 | +Italian | +
| it-IT | +0x0410 | +Italian - Italy | +
| it-CH | +0x0810 | +Italian - Switzerland | +
| ja | +0x0011 | +Japanese | +
| ja-JP | +0x0411 | +Japanese - Japan | +
| kn | +0x004B | +Kannada | +
| kn-IN | +0x044B | +Kannada - India | +
| kk | +0x003F | +Kazakh | +
| kk-KZ | +0x043F | +Kazakh - Kazakhstan | +
| kok | +0x0057 | +Konkani | +
| kok-IN | +0x0457 | +Konkani - India | +
| ko | +0x0012 | +Korean | +
| ko-KR | +0x0412 | +Korean - Korea | +
| ky | +0x0040 | +Kyrgyz | +
| ky-KZ | +0x0440 | +Kyrgyz - Kazakhstan | +
| lv | +0x0026 | +Latvian | +
| lv-LV | +0x0426 | +Latvian - Latvia | +
| lt | +0x0027 | +Lithuanian | +
| lt-LT | +0x0427 | +Lithuanian - Lithuania | +
| mk | +0x002F | +Macedonian | +
| mk-MK | +0x042F | +Macedonian - FYROM | +
| ms | +0x003E | +Malay | +
| ms-BN | +0x083E | +Malay - Brunei | +
| ms-MY | +0x043E | +Malay - Malaysia | +
| mr | +0x004E | +Marathi | +
| mr-IN | +0x044E | +Marathi - India | +
| mn | +0x0050 | +Mongolian | +
| mn-MN | +0x0450 | +Mongolian - Mongolia | +
| no | +0x0014 | +Norwegian | +
| nb-NO | +0x0414 | +Norwegian (Bokmål) - Norway | +
| nn-NO | +0x0814 | +Norwegian (Nynorsk) - Norway | +
| pl | +0x0015 | +Polish | +
| pl-PL | +0x0415 | +Polish - Poland | +
| pt | +0x0016 | +Portuguese | +
| pt-BR | +0x0416 | +Portuguese - Brazil | +
| pt-PT | +0x0816 | +Portuguese - Portugal | +
| pa | +0x0046 | +Punjabi | +
| pa-IN | +0x0446 | +Punjabi - India | +
| ro | +0x0018 | +Romanian | +
| ro-RO | +0x0418 | +Romanian - Romania | +
| ru | +0x0019 | +Russian | +
| ru-RU | +0x0419 | +Russian - Russia | +
| sa | +0x004F | +Sanskrit | +
| sa-IN | +0x044F | +Sanskrit - India | +
| sr-SP-Cyrl | +0x0C1A | +Serbian (Cyrillic) - Serbia | +
| sr-SP-Latn | +0x081A | +Serbian (Latin) - Serbia | +
| sk | +0x001B | +Slovak | +
| sk-SK | +0x041B | +Slovak - Slovakia | +
| sl | +0x0024 | +Slovenian | +
| sl-SI | +0x0424 | +Slovenian - Slovenia | +
| es | +0x000A | +Spanish | +
| es-AR | +0x2C0A | +Spanish - Argentina | +
| es-BO | +0x400A | +Spanish - Bolivia | +
| es-CL | +0x340A | +Spanish - Chile | +
| es-CO | +0x240A | +Spanish - Colombia | +
| es-CR | +0x140A | +Spanish - Costa Rica | +
| es-DO | +0x1C0A | +Spanish - Dominican Republic | +
| es-EC | +0x300A | +Spanish - Ecuador | +
| es-SV | +0x440A | +Spanish - El Salvador | +
| es-GT | +0x100A | +Spanish - Guatemala | +
| es-HN | +0x480A | +Spanish - Honduras | +
| es-MX | +0x080A | +Spanish - Mexico | +
| es-NI | +0x4C0A | +Spanish - Nicaragua | +
| es-PA | +0x180A | +Spanish - Panama | +
| es-PY | +0x3C0A | +Spanish - Paraguay | +
| es-PE | +0x280A | +Spanish - Peru | +
| es-PR | +0x500A | +Spanish - Puerto Rico | +
| es-ES | +0x0C0A | +Spanish - Spain | +
| es-UY | +0x380A | +Spanish - Uruguay | +
| es-VE | +0x200A | +Spanish - Venezuela | +
| sw | +0x0041 | +Swahili | +
| sw-KE | +0x0441 | +Swahili - Kenya | +
| sv | +0x001D | +Swedish | +
| sv-FI | +0x081D | +Swedish - Finland | +
| sv-SE | +0x041D | +Swedish - Sweden | +
| syr | +0x005A | +Syriac | +
| syr-SY | +0x045A | +Syriac - Syria | +
| ta | +0x0049 | +Tamil | +
| ta-IN | +0x0449 | +Tamil - India | +
| tt | +0x0044 | +Tatar | +
| tt-RU | +0x0444 | +Tatar - Russia | +
| te | +0x004A | +Telugu | +
| te-IN | +0x044A | +Telugu - India | +
| th | +0x001E | +Thai | +
| th-TH | +0x041E | +Thai - Thailand | +
| tr | +0x001F | +Turkish | +
| tr-TR | +0x041F | +Turkish - Turkey | +
| uk | +0x0022 | +Ukrainian | +
| uk-UA | +0x0422 | +Ukrainian - Ukraine | +
| ur | +0x0020 | +Urdu | +
| ur-PK | +0x0420 | +Urdu - Pakistan | +
| uz | +0x0043 | +Uzbek | +
| uz-UZ-Cyrl | +0x0843 | +Uzbek (Cyrillic) - Uzbekistan | +
| uz-UZ-Latn | +0x0443 | +Uzbek (Latin) - Uzbekistan | +
| vi | +0x002A | +Vietnamese | +
| vi-VN | +0x042A | +Vietnamese - Vietnam | +
+ Note Both the SelectedDate property and the + SelectedDates collection are updated before the SelectionChanged + event is raised. You can override the date selection by using the + OnSelectionChanged event handler to manually set the + SelectedDate property. The SelectionChanged event does not get + raised when this property is programmatically set. ++
+ Note Both the SelectedDate property and the + SelectedDates collection are updated before the SelectionChanged + event is raised.You can override the dates selection by using the + OnSelectionChanged event handler to manually set the + SelectedDates collection. The SelectionChanged event is not + raised when this collection is programmatically set. ++
+ (this);
+ _weakEventListener.OnEventAction = (instance, source, eventArgs) =>
+ instance.OnCollectionChanged(source, eventArgs);
+ _weakEventListener.OnDetachAction = (weakEventListener) =>
+ newNotifyCollectionChanged.CollectionChanged -= weakEventListener.OnEvent;
+ newNotifyCollectionChanged.CollectionChanged += _weakEventListener.OnEvent;
+ ]]>
+
+
+ void Page_Load(object sender, EventArgs e)
+ {
+ RadProgressBar myProgressBar = new RadProgressBar();
+ myProgressBar.Orientation = Orientation.Vertical;
+ }
+
+
+
+ ]]>
+
+
+
+
+
+
+ ]]>
+
+
+
+
+
+
+ ]]>
+
+
+
+
+
+
+ ]]>
+
+
+
+
+
+
+ ]]>
+
+
+
+
+ SetAttribute(name) it will be displayed as a boolean
+ property; when set using SetAttribute(name,"") it will be displayed as an empty
+ string.
+ DocumentModeHanler. This mode
+ is meant for doing surveys on existing content.
+ Tokenizer reports tokens through this interface.
+ Tokenizer in order to set the content
+ model flag and in order to be able to query for Locator data.
+ wantsComments()
+ returned false.
+ true.
+ If not, return false
+ xmlns prefix in the SVG and MathML modes.
+ xlink
+ prefix in the SVG and MathML modes.
+ xml prefix in the SVG and MathML modes.
+ AttributeName corresponding to the argument dataAttributeName for a local name.
+ AttributeNamethis in Java and for non-dynamic instances in C++.
+ xmlns attribute.
+ xmlns attribute; otherwise, wantsComments()
+ returned false.
+ null.
+
+ @param context
+ null.
+
+ @param context
+ delete on the returned object.
+ true.
+ If not, return false
+ AttributeName ownership transfer
+ in the isindex case to avoid freeing custom names twice in C++.
+ releaseLocal on the return value balances the refcount set by this method.
+ StackNode
+ because in C++ the caller is reponsible for reobtaining the local names
+ from another interner.
+ Locator interface. This is not an
+ incidental implementation detail: Users of this class are encouraged to make
+ use of the Locator nature.
+ By default, the tokenizer may report data that XML 1.0 bans. The tokenizer
+ can be configured to treat these conditions as fatal or to coerce the infoset
+ to something that XML 1.0 allows.
+ char[]
+ char[]
+ char[]
+ char[]
+ chars in strBuf.
+ chars in longStrBuf.
+ true if tokenizing an end tag
+ true when HTML4-specific additional errors are requested.
+ TransitionHandler.
+ UTF16Buffer.
+ UTF16Buffer.
+ true, then a new paragraph with same formatting will be inserted.
+ Otherwise, only the inlines from that paragraph will be inserted.true.string values
+ are equal.
+
+ @param value1 The left-hand side of the encoded {@link string#equals(Object)}.
+ @param value2 The right-hand side of the encoded {@link string#equals(Object)}.
+ @return true if the encoded strings are equal;
+ false otherwise.
+ @see #isDoubleMetaphoneEqual(string,string,bool)
+ string values
+ are equal, optionally using the alternate value.
+
+ @param value1 The left-hand side of the encoded {@link string#equals(Object)}.
+ @param value2 The right-hand side of the encoded {@link string#equals(Object)}.
+ @param alternate use the alternate value if true.
+ @return true if the encoded strings are equal;
+ false otherwise.
+ true if the value starts with any of 'GN', 'KN',
+ 'PN', 'WR' or 'PS'.
+ index if available, otherwise
+ it returns Character.MIN_VALUE so that there is some sort
+ of a default
+ value contains any of the criteria
+ starting
+ * at index start and matching up to length length
+
+ System.Environment.SetEnvironmentVariable("VsSDKToolsPath", System.IO.Path.GetFullPath(ProjectDirectory + @"\..\tools\VSSDK\bin"),EnvironmentVariableTarget.Process);
+ System.Environment.SetEnvironmentVariable("VsSDKSchemaDir", System.IO.Path.GetFullPath(ProjectDirectory + @"\..\tools\VSSDK\schemas"),EnvironmentVariableTarget.Process);
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters, p => p.StartsWith("W"))));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var capturedValue = string.Empty;
+ var match = new CaptureMatch{string}(x => capturedValue = x);
+ mock.Setup(x => x.DoSomething(Capture.With(match)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", capturedValue);
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true);
+
+
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback((string command) => Console.WriteLine(command));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true)
+ .Returns(true);
+
+ Note that in the case of value-returning methods, after the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback(command => Console.WriteLine(command))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ var mock = new Mock<IContainer>();
+
+ mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
+ .Raises(add => add.Added += null, EventArgs.Empty);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the method
+ is executed and the value the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Returns((string command) => returnValues[command]);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2) => arg1 + arg2);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3) => arg1 + arg2 + arg3);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4) => arg1 + arg2 + arg3 + arg4);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5) => arg1 + arg2 + arg3 + arg4 + arg5);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16);
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMostOnce();
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMost( 5 );
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the property
+ is retrieved and the value the
+ mock.SetupGet(x => x.Suspended)
+ .Callback(() => called = true)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended)
+ .Callback((bool state) => Console.WriteLine(state));
+
+
+ mock.Setup(x => x.Execute(""))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.Execute(""))
+ .Throws<ArgumentException>();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable("Ping should be executed always!");
+
+
+ // Typed instance, not the mock, is retrieved from some test API.
+ HttpContextBase context = GetMockContext();
+
+ // context.Request is the typed object from the "real" API
+ // so in order to add a setup to it, we need to get
+ // the mock that "owns" it
+ Mock<HttpRequestBase> request = Mock.Get(context.Request);
+ mock.Setup(req => req.AppRelativeCurrentExecutionFilePath)
+ .Returns(tempUrl);
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+ // add IDisposable interface
+ var disposable = mock.As<IDisposable>();
+ disposable.Setup(d => d.Dispose()).Verifiable();
+
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ repository.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the repository
+ to create loose mocks and later verify only verifiable setups:
+
+ var repository = new MockRepository(MockBehavior.Loose);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // this setup will be verified when we verify the repository
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the repository
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ repository.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the repository with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = repository.Create<IFoo>(MockBehavior.Loose);
+ var bar = repository.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ repository.Verify();
+
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ Note that the return value from the compiler matcher is irrelevant.
+ This method will never be called, and is just used to satisfy the
+ compiler and to signal Moq that this is not a method that we want
+ to be invoked at runtime.
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+
+ At runtime, the mocked method will be invoked with a specific
+ list of orders. This value will be passed to this runtime
+ matcher as the first argument, while the second argument is the
+ one specified in the setup (
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+ }
+
+ And the concrete test using this matcher:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ // use mock, invoke Save, and have the matcher filter.
+
+
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ mock.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.True(order.IsFilled);
+
+ The following example shows how to use the
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ // shows how to expect a value within a range
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+ // shows how to throw for unexpected calls.
+ mock.Setup(x => x.Remove(
+ It.IsAny<string>(),
+ It.IsAny<int>()))
+ .Throws(new InvalidOperationException());
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.False(order.IsFilled);
+
+ var mock = new Mock<IFormatProvider>();
+ var mock = new Mock<MyProvider>(someArgument, 25);
+ var mock = new Mock<IFormatProvider>(MockBehavior.Relaxed);
+ var mock = new Mock<MyProvider>(someArgument, 25);
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+
+ mock.Setup(x => x.HasInventory("Talisker", 50)).Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.Stub(v => v.Value);
+
+ After the
+ IHaveValue v = mock.Object;
+
+ v.Value = 5;
+ Assert.Equal(5, v.Value);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.SetupProperty(v => v.Value, 5);
+
+ After the
+ IHaveValue v = mock.Object;
+ // Initial value was stored
+ Assert.Equal(5, v.Value);
+
+ // New value set which changes the initial value
+ v.Value = 6;
+ Assert.Equal(6, v.Value);
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50), "When filling orders, inventory has to be checked");
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true, "Warehouse should always be closed after the action");
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));
+
+
+ var mockView = new Mock<IOrdersView>();
+ var presenter = new OrdersPresenter(mockView.Object);
+
+ // Check that the presenter has no selection by default
+ Assert.Null(presenter.SelectedOrder);
+
+ // Raise the event with a specific arguments data
+ mockView.Raise(v => v.SelectionChanged += null, new OrderEventArgs { Order = new Order("moq", 500) });
+
+ // Now the presenter reacted to the event, and we have a selected order
+ Assert.NotNull(presenter.SelectedOrder);
+ Assert.Equal("moq", presenter.SelectedOrder.ProductName);
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.MyEvent -= null, "Name", bool, 25);
+
+
+ mock.SetupSet(x => x.Suspended);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ factory.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the factory
+ to create loose mocks and later verify only verifiable setups:
+
+ var factory = new MockFactory(MockBehavior.Loose);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // this setup will be verified when we verify the factory
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the factory
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ factory.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the factory with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+ var bar = factory.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ // use mock on tests
+
+ factory.VerifyAll();
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>("Foo", 25, true);
+ // use mock on tests
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>(MockBehavior.Strict, "Foo", 25, true);
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());
+
+
+ mock.Setup(x => x.Do(It.Is<int>(i => i % 2 == 0)))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Setup(x => x.GetUser(It.Is<int>(i => i < 0)))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(1, 2, 3)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(1, 2, 3)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+"))).Returns(1);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase))).Returns(1);
+
+
+ // Throws an exception for a call to Remove with a null string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsNull<string>())
+ .Throws(new InvalidOperationException());
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsAny<string>())
+ .Throws(new InvalidOperationException());
+
+
+ mock.Protected()
+ .Setup("Do", ItExpr.Is<int>(i => i % 2 == 0))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Protected()
+ .Setup("GetUser", ItExpr.Is<int>(i => i < 0))
+ .Throws(new ArgumentException());
+
+
+ mock.Protected()
+ .Setup("HasInventory",
+ ItExpr.IsAny<string>(),
+ ItExpr.IsInRange(0, 100, Range.Inclusive))
+ .Returns(false);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+"))
+ .Returns(1);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+", RegexOptions.IgnoreCase))
+ .Returns(1);
+
+
+ [Matcher]
+ public Order IsBigOrder()
+ {
+ return Match<Order>.Create(
+ o => o.GrandTotal >= 5000,
+ /* a friendly expression to render on failures */
+ () => IsBigOrder());
+ }
+
+ This method can be used in any mock setup invocation:
+
+ mock.Setup(m => m.Submit(IsBigOrder()).Throws<UnauthorizedAccessException>();
+
+ At runtime, Moq knows that the return value was a matcher (note that the method MUST be
+ annotated with the [Matcher] attribute in order to determine this) and
+ evaluates your predicate with the actual value passed into your predicate.
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var parameters = new List{string}();
+ mock.Setup(x => x.DoSomething(Capture.In(parameters, p => p.StartsWith("W"))));
+
+ Assert code:
+
+ Assert.Equal("Hello!", parameters.Single());
+
+
+ var capturedValue = string.Empty;
+ var match = new CaptureMatch{string}(x => capturedValue = x);
+ mock.Setup(x => x.DoSomething(Capture.With(match)));
+
+ Assert code:
+
+ Assert.Equal("Hello!", capturedValue);
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());
+
+
+ mock.Setup(x => x.Do(It.Is<int>(i => i % 2 == 0)))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Setup(x => x.GetUser(It.Is<int>(i => i < 0)))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsIn(1, 2, 3)))
+ .Returns(false);
+
+
+ var values = new List<int> { 1, 2, 3 };
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(values)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsNotIn(1, 2, 3)))
+ .Returns(false);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+"))).Returns(1);
+
+
+ mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase))).Returns(1);
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true);
+
+
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback((string command) => Console.WriteLine(command));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ var called = false;
+ mock.Setup(x => x.Execute())
+ .Callback(() => called = true)
+ .Returns(true);
+
+ Note that in the case of value-returning methods, after the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Callback(command => Console.WriteLine(command))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2) => Console.WriteLine(arg1 + arg2));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3) => Console.WriteLine(arg1 + arg2 + arg3));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4) => Console.WriteLine(arg1 + arg2 + arg3 + arg4));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15));
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>(),
+ It.IsAny<string>()))
+ .Callback((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16) => Console.WriteLine(arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16));
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Callback(() => called = true)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended)
+ .Callback((bool state) => Console.WriteLine(state));
+
+
+ var mock = new Mock<IContainer>();
+
+ mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
+ .Raises(add => add.Added += null, EventArgs.Empty);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(true);
+
+
+ mock.Setup(x => x.Execute("ping"))
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the method
+ is executed and the value the
+ mock.Setup(x => x.Execute(It.IsAny<string>()))
+ .Returns((string command) => returnValues[command]);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2) => arg1 + arg2);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3) => arg1 + arg2 + arg3);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4) => arg1 + arg2 + arg3 + arg4);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5) => arg1 + arg2 + arg3 + arg4 + arg5);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15);
+
+
+ mock.Setup(x => x.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<int>()))
+ .Returns((string arg1, string arg2, string arg3, string arg4, string arg5, string arg6, string arg7, string arg8, string arg9, string arg10, string arg11, string arg12, string arg13, string arg14, string arg15, string arg16) => arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9 + arg10 + arg11 + arg12 + arg13 + arg14 + arg15 + arg16);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(() => returnValues[0]);
+
+ The lambda expression to retrieve the return value is lazy-executed,
+ meaning that its value may change depending on the moment the property
+ is retrieved and the value the
+ mock.Setup(x => x.Execute(""))
+ .Throws(new ArgumentException());
+
+
+ mock.Setup(x => x.Execute(""))
+ .Throws<ArgumentException>();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable();
+
+
+ mock.Expect(x => x.Execute("ping"))
+ .Returns(true)
+ .Verifiable("Ping should be executed always!");
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMostOnce();
+
+
+ var mock = new Mock<ICommand>();
+ mock.Setup(foo => foo.Execute("ping"))
+ .AtMost( 5 );
+
+
+ // Typed instance, not the mock, is retrieved from some test API.
+ HttpContextBase context = GetMockContext();
+
+ // context.Request is the typed object from the "real" API
+ // so in order to add a setup to it, we need to get
+ // the mock that "owns" it
+ Mock<HttpRequestBase> request = Mock.Get(context.Request);
+ mock.Setup(req => req.AppRelativeCurrentExecutionFilePath)
+ .Returns(tempUrl);
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Verifiable().Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory.
+ this.Verify();
+
+
+ var mock = new Mock<IWarehouse>();
+ this.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+ ...
+ // other test code
+ ...
+ // Will throw if the test code has didn't call HasInventory, even
+ // that expectation was not marked as verifiable.
+ this.VerifyAll();
+
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+ // add IDisposable interface
+ var disposable = mock.As<IDisposable>();
+ disposable.Setup(d => d.Dispose()).Verifiable();
+
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ repository.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the repository
+ to create loose mocks and later verify only verifiable setups:
+
+ var repository = new MockRepository(MockBehavior.Loose);
+
+ var foo = repository.Create<IFoo>();
+ var bar = repository.Create<IBar>();
+
+ // this setup will be verified when we verify the repository
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the repository
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ repository.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the repository with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var repository = new MockRepository(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = repository.Create<IFoo>(MockBehavior.Loose);
+ var bar = repository.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ repository.Verify();
+
+
+ [Matcher]
+ public Order IsBigOrder()
+ {
+ return Match<Order>.Create(
+ o => o.GrandTotal >= 5000,
+ /* a friendly expression to render on failures */
+ () => IsBigOrder());
+ }
+
+ This method can be used in any mock setup invocation:
+
+ mock.Setup(m => m.Submit(IsBigOrder()).Throws<UnauthorizedAccessException>();
+
+ At runtime, Moq knows that the return value was a matcher (note that the method MUST be
+ annotated with the [Matcher] attribute in order to determine this) and
+ evaluates your predicate with the actual value passed into your predicate.
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return Match<IEnumerable<Order>>.Create(orders => orders.Contains(order));
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+ }
+
+ Now we can invoke this static method instead of an argument in an
+ invocation:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ Note that the return value from the compiler matcher is irrelevant.
+ This method will never be called, and is just used to satisfy the
+ compiler and to signal Moq that this is not a method that we want
+ to be invoked at runtime.
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+
+ At runtime, the mocked method will be invoked with a specific
+ list of orders. This value will be passed to this runtime
+ matcher as the first argument, while the second argument is the
+ one specified in the setup (
+ public static class Orders
+ {
+ [Matcher]
+ public static IEnumerable<Order> Contains(Order order)
+ {
+ return null;
+ }
+
+ public static bool Contains(IEnumerable<Order> orders, Order order)
+ {
+ return orders.Contains(order);
+ }
+ }
+
+ And the concrete test using this matcher:
+
+ var order = new Order { ... };
+ var mock = new Mock<IRepository<Order>>();
+
+ mock.Setup(x => x.Save(Orders.Contains(order)))
+ .Throws<ArgumentException>();
+
+ // use mock, invoke Save, and have the matcher filter.
+
+
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ mock.Setup(x => x.HasInventory(TALISKER, 50)).Returns(true);
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.True(order.IsFilled);
+
+ The following example shows how to use the
+ // Arrange
+ var order = new Order(TALISKER, 50);
+ var mock = new Mock<IWarehouse>();
+
+ // shows how to expect a value within a range
+ mock.Setup(x => x.HasInventory(
+ It.IsAny<string>(),
+ It.IsInRange(0, 100, Range.Inclusive)))
+ .Returns(false);
+
+ // shows how to throw for unexpected calls.
+ mock.Setup(x => x.Remove(
+ It.IsAny<string>(),
+ It.IsAny<int>()))
+ .Throws(new InvalidOperationException());
+
+ // Act
+ order.Fill(mock.Object);
+
+ // Assert
+ Assert.False(order.IsFilled);
+
+ var mock = new Mock<IFormatProvider>();
+ var mock = new Mock<MyProvider>(someArgument, 25);
+ var mock = new Mock<IFormatProvider>(MockBehavior.Relaxed);
+ var mock = new Mock<MyProvider>(someArgument, 25);
+
+ var mock = new Mock<IProcessor>();
+ mock.Setup(x => x.Execute("ping"));
+
+
+ mock.Setup(x => x.HasInventory("Talisker", 50)).Returns(true);
+
+
+ mock.SetupGet(x => x.Suspended)
+ .Returns(true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ mock.SetupSet(x => x.Suspended = true);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.Stub(v => v.Value);
+
+ After the
+ IHaveValue v = mock.Object;
+
+ v.Value = 5;
+ Assert.Equal(5, v.Value);
+
+
+ var mock = new Mock<IHaveValue>();
+ mock.SetupProperty(v => v.Value, 5);
+
+ After the
+ IHaveValue v = mock.Object;
+ // Initial value was stored
+ Assert.Equal(5, v.Value);
+
+ // New value set which changes the initial value
+ v.Value = 6;
+ Assert.Equal(6, v.Value);
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IProcessor>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call Execute with a "ping" string argument.
+ mock.Verify(proc => proc.Execute("ping"));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50));
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't call HasInventory.
+ mock.Verify(warehouse => warehouse.HasInventory(TALISKER, 50), "When filling orders, inventory has to be checked");
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't retrieve the IsClosed property.
+ mock.VerifyGet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed = true, "Warehouse should always be closed after the action");
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.PropertyChanged -= null, new PropertyChangedEventArgs("Name"));
+
+
+ var mockView = new Mock<IOrdersView>();
+ var presenter = new OrdersPresenter(mockView.Object);
+
+ // Check that the presenter has no selection by default
+ Assert.Null(presenter.SelectedOrder);
+
+ // Raise the event with a specific arguments data
+ mockView.Raise(v => v.SelectionChanged += null, new OrderEventArgs { Order = new Order("moq", 500) });
+
+ // Now the presenter reacted to the event, and we have a selected order
+ Assert.NotNull(presenter.SelectedOrder);
+ Assert.Equal("moq", presenter.SelectedOrder.ProductName);
+
+
+ var mock = new Mock<IViewModel>();
+
+ mock.Raise(x => x.MyEvent -= null, "Name", bool, 25);
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // no need to call Verifiable() on the setup
+ // as we'll be validating all of them anyway.
+ foo.Setup(f => f.Do());
+ bar.Setup(b => b.Redo());
+
+ // exercise the mocks here
+
+ factory.VerifyAll();
+ // At this point all setups are already checked
+ // and an optional MockException might be thrown.
+ // Note also that because the mocks are strict, any invocation
+ // that doesn't have a matching setup will also throw a MockException.
+
+ The following examples shows how to setup the factory
+ to create loose mocks and later verify only verifiable setups:
+
+ var factory = new MockFactory(MockBehavior.Loose);
+
+ var foo = factory.Create<IFoo>();
+ var bar = factory.Create<IBar>();
+
+ // this setup will be verified when we verify the factory
+ foo.Setup(f => f.Do()).Verifiable();
+
+ // this setup will NOT be verified
+ foo.Setup(f => f.Calculate());
+
+ // this setup will be verified when we verify the factory
+ bar.Setup(b => b.Redo()).Verifiable();
+
+ // exercise the mocks here
+ // note that because the mocks are Loose, members
+ // called in the interfaces for which no matching
+ // setups exist will NOT throw exceptions,
+ // and will rather return default values.
+
+ factory.Verify();
+ // At this point verifiable setups are already checked
+ // and an optional MockException might be thrown.
+
+ The following examples shows how to setup the factory with a
+ default strict behavior, overriding that default for a
+ specific mock:
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ // this particular one we want loose
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+ var bar = factory.Create<IBar>();
+
+ // specify setups
+
+ // exercise the mocks here
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>();
+ // use mock on tests
+
+ factory.VerifyAll();
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>("Foo", 25, true);
+ // use mock on tests
+
+ factory.Verify();
+
+
+ var factory = new MockFactory(MockBehavior.Strict);
+
+ var foo = factory.Create<IFoo>(MockBehavior.Loose);
+
+
+ var factory = new MockFactory(MockBehavior.Default);
+
+ var mock = factory.Create<MyBase>(MockBehavior.Strict, "Foo", 25, true);
+
+
+ mock.SetupSet(x => x.Suspended);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ var mock = new Mock<IWarehouse>();
+ // exercise mock
+ //...
+ // Will throw if the test code didn't set the IsClosed property.
+ mock.VerifySet(warehouse => warehouse.IsClosed);
+
+
+ // Throws an exception for a call to Remove with a null string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsNull<string>())
+ .Throws(new InvalidOperationException());
+
+
+ // Throws an exception for a call to Remove with any string value.
+ mock.Protected()
+ .Setup("Remove", ItExpr.IsAny<string>())
+ .Throws(new InvalidOperationException());
+
+
+ mock.Protected()
+ .Setup("Do", ItExpr.Is<int>(i => i % 2 == 0))
+ .Returns(1);
+
+ This example shows how to throw an exception if the argument to the
+ method is a negative number:
+
+ mock.Protected()
+ .Setup("GetUser", ItExpr.Is<int>(i => i < 0))
+ .Throws(new ArgumentException());
+
+
+ mock.Protected()
+ .Setup("HasInventory",
+ ItExpr.IsAny<string>(),
+ ItExpr.IsInRange(0, 100, Range.Inclusive))
+ .Returns(false);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+"))
+ .Returns(1);
+
+
+ mock.Protected()
+ .Setup("Check", ItExpr.IsRegex("[a-z]+", RegexOptions.IgnoreCase))
+ .Returns(1);
+
+
+ private async Task SomeOperationAsync() {
+ // on the caller's thread.
+ await DoAsync();
+
+ // Now switch to a threadpool thread explicitly.
+ await TaskScheduler.Default;
+
+ // Now switch to the Main thread to talk to some STA object.
+ await this.JobContext.SwitchToMainThreadAsync();
+ STAService.DoSomething();
+ }
+
+
+ // On threadpool or Main thread, this method will block
+ // the calling thread until all async operations in the
+ // delegate complete.
+ this.JobContext.RunSynchronously(async delegate {
+ // still on the threadpool or Main thread as before.
+ await OperationAsync();
+ // still on the threadpool or Main thread as before.
+ await Task.Run(async delegate {
+ // Now we're on a threadpool thread.
+ await Task.Yield();
+ // still on a threadpool thread.
+ });
+ // Now back on the Main thread (or threadpool thread if that's where we started).
+ });
+
+
+ await JoinableTaskFactory.SwitchToMainThreadAsync();
+
+ b) Have a synchronous signature, and throw an exception when called on the wrong thread.
+ In particular, no method is allowed to synchronously marshal work to another thread
+ (blocking while that work is done). Synchronous blocks in general are to be avoided
+ whenever possible.
+ 2. When an implementation of an already-shipped public API must call asynchronous code
+ and block for its completion, it must do so by following this simple pattern:
+
+ JoinableTaskFactory.Run(async delegate {
+ await SomeOperationAsync(...);
+ });
+
+ 3. If ever awaiting work that was started earlier, that work must be Joined.
+ For example, one service kicks off some asynchronous work that may later become
+ synchronously blocking:
+
+ JoinableTask longRunningAsyncWork = JoinableTaskFactory.RunAsync(async delegate {
+ await SomeOperationAsync(...);
+ });
+
+ Then later that async work becomes blocking:
+
+ longRunningAsyncWork.Join();
+
+ or perhaps:
+
+ await longRunningAsyncWork;
+
+ Note however that this extra step is not necessary when awaiting is done
+ immediately after kicking off an asynchronous operation.
+
+ this.JoinableTaskContext.RunSynchronously(async delegate {
+ using(this.JoinableTaskContext.SuppressRelevance()) {
+ var asyncOperation = Task.Run(async delegate {
+ // Some background work.
+ await this.JoinableTaskContext.SwitchToMainThreadAsync();
+ // Some Main thread work, that cannot begin until the outer RunSynchronously call has returned.
+ });
+ }
+
+ // Because the asyncOperation is not related to this Main thread work (it was suppressed),
+ // the following await *would* deadlock if it were uncommented.
+ ////await asyncOperation;
+ });
+
+
+ this.JoinableTaskContext.RunSynchronously(async delegate {
+ using(this.JoinableTaskContext.SuppressRelevance()) {
+ var asyncOperation = Task.Run(async delegate {
+ // Some background work.
+ await this.JoinableTaskContext.SwitchToMainThreadAsync();
+ // Some Main thread work, that cannot begin until the outer RunSynchronously call has returned.
+ });
+ }
+
+ // Because the asyncOperation is not related to this Main thread work (it was suppressed),
+ // the following await *would* deadlock if it were uncommented.
+ ////await asyncOperation;
+ });
+
+ Math.Max( , ).
+ When setting this property, Math.Min( , ).
+ The caret is not scrolled into view.
+ Math.Min( , ).
+ When setting this property, Math.Max( , ).
+ The caret is not scrolled into view.
+