- 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);
-
-
- // 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<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();
-
-
- 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.Setup(x => x.Execute(""))
- .Throws(new ArgumentException());
-
-
- mock.Setup(x => x.Execute(""))
- .Throws<ArgumentException>();
-
-
- 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.Expect(x => x.Execute("ping"))
- .Returns(true)
- .Verifiable();
-
-
- mock.Expect(x => x.Execute("ping"))
- .Returns(true)
- .Verifiable("Ping should be executed always!");
-
-
- mock.SetupGet(x => x.Suspended)
- .Callback(() => called = true)
- .Returns(true);
-
-
- 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
- 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);
-
-
- mock.SetupSet(x => x.Suspended)
- .Callback((bool state) => Console.WriteLine(state));
-
-
- // 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);
-
-
- 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 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);
-
-
- // 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<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();
-
-
- 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.Setup(x => x.Execute(""))
- .Throws(new ArgumentException());
-
-
- mock.Setup(x => x.Execute(""))
- .Throws<ArgumentException>();
-
-
- 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.Expect(x => x.Execute("ping"))
- .Returns(true)
- .Verifiable();
-
-
- mock.Expect(x => x.Execute("ping"))
- .Returns(true)
- .Verifiable("Ping should be executed always!");
-
-
- mock.SetupGet(x => x.Suspended)
- .Callback(() => called = true)
- .Returns(true);
-
-
- 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
- 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);
-
-
- mock.SetupSet(x => x.Suspended)
- .Callback((bool state) => Console.WriteLine(state));
-
-
- // 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);
-
-
- 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>();
-
-
- // 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);
-
-
- // 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<IProcessor>();
- mock.Setup(x => x.Execute("ping"));
-
- // add IDisposable interface
- var disposable = mock.As<IDisposable>();
- disposable.Setup(d => d.Dispose()).Verifiable();
-
- 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);
-
-
- // 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);
-
-
- mock.Setup(x => x.Check(It.IsRegex("[a-z]+"))).Returns(1);
-
-
- mock.Setup(x => x.Check(It.IsRegex("[a-z]+", RegexOptions.IgnoreCase))).Returns(1);
-
-
- 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);
-
-
- mock.Setup(x => x.Execute(It.IsAny<string>()))
- .Callback((string command) => Console.WriteLine(command));
-
-
- 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 );
-
-
- var mock = new Mock<IContainer>();
-
- mock.Setup(add => add.Add(It.IsAny<string>(), It.IsAny<object>()))
- .Raises(add => add.Added += null, EventArgs.Empty);
-
-
- mock.Expect(x => x.Execute("ping"))
- .Returns(true)
- .Verifiable();
-
-
- mock.Expect(x => x.Execute("ping"))
- .Returns(true)
- .Verifiable("Ping should be executed always!");
-
-
- mock.Setup(x => x.Execute(""))
- .Throws(new ArgumentException());
-
-
- mock.Setup(x => x.Execute(""))
- .Throws<ArgumentException>();
-
-
- 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 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<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.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.SetupGet(x => x.Suspended)
- .Callback(() => called = true)
- .Returns(true);
-
-
- 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.SetupSet(x => x.Suspended)
- .Callback((bool state) => Console.WriteLine(state));
-
-
- 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();
-
-
- 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);
-
-
- [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.
-
-
- 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);
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- Condition | -Foreground Color | -Background Color | -
---|---|---|
level == LogLevel.Fatal | -Red | -NoChange | -
level == LogLevel.Error | -Yellow | -NoChange | -
level == LogLevel.Warn | -Magenta | -NoChange | -
level == LogLevel.Info | -White | -NoChange | -
level == LogLevel.Debug | -Gray | -NoChange | -
level == LogLevel.Trace | -DarkGray | -NoChange | -
- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
-
-
-
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${basedir}/${level}.log
- All
- Caution: Enabling this option can considerably slow down your file
- logging in multi-process scenarios. If only one process is going to
- be writing to the file, consider setting
- To set up the target in the configuration file, - use the following syntax: -
-
- - The result is: -
-- To set up the log target programmatically similar to above use code like this: -
-
,
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - Mail target works best when used with BufferingWrapper target - which lets you send multiple log messages in single mail -
-- To set up the buffered mail target in the configuration file, - use the following syntax: -
-
- - To set up the buffered mail target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The result is: -
- To set up the target with coloring rules in the configuration file, - use the following syntax: -
-
-
-
- - The result is: -
- To set up the log target programmatically similar to above use code like this: -
-
- ,
-
-
- for RowColoring,
-
-
- for WordColoring
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- Condition | -Foreground Color | -Background Color | -
---|---|---|
level == LogLevel.Fatal | -Red | -NoChange | -
level == LogLevel.Error | -Yellow | -NoChange | -
level == LogLevel.Warn | -Magenta | -NoChange | -
level == LogLevel.Info | -White | -NoChange | -
level == LogLevel.Debug | -Gray | -NoChange | -
level == LogLevel.Trace | -DarkGray | -NoChange | -
- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
-
-
-
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${basedir}/${level}.log
- All
- Caution: Enabling this option can considerably slow down your file
- logging in multi-process scenarios. If only one process is going to
- be writing to the file, consider setting
- To set up the target in the configuration file, - use the following syntax: -
-
- - The result is: -
-- To set up the log target programmatically similar to above use code like this: -
-
,
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - Mail target works best when used with BufferingWrapper target - which lets you send multiple log messages in single mail -
-- To set up the buffered mail target in the configuration file, - use the following syntax: -
-
- - To set up the buffered mail target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The result is: -
- To set up the target with coloring rules in the configuration file, - use the following syntax: -
-
-
-
- - The result is: -
- To set up the log target programmatically similar to above use code like this: -
-
- ,
-
-
- for RowColoring,
-
-
- for WordColoring
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- Condition | -Foreground Color | -Background Color | -
---|---|---|
level == LogLevel.Fatal | -Red | -NoChange | -
level == LogLevel.Error | -Yellow | -NoChange | -
level == LogLevel.Warn | -Magenta | -NoChange | -
level == LogLevel.Info | -White | -NoChange | -
level == LogLevel.Debug | -Gray | -NoChange | -
level == LogLevel.Trace | -DarkGray | -NoChange | -
- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
-
-
-
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${basedir}/${level}.log
- All
- Caution: Enabling this option can considerably slow down your file
- logging in multi-process scenarios. If only one process is going to
- be writing to the file, consider setting
- To set up the target in the configuration file, - use the following syntax: -
-
- - The result is: -
-- To set up the log target programmatically similar to above use code like this: -
-
,
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - Mail target works best when used with BufferingWrapper target - which lets you send multiple log messages in single mail -
-- To set up the buffered mail target in the configuration file, - use the following syntax: -
-
- - To set up the buffered mail target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The result is: -
- To set up the target with coloring rules in the configuration file, - use the following syntax: -
-
-
-
- - The result is: -
- To set up the log target programmatically similar to above use code like this: -
-
- ,
-
-
- for RowColoring,
-
-
- for WordColoring
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will crawl. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To print the results, use any application that's able to receive messages over - TCP or UDP. NetCat is - a simple but very powerful command-line tool that can be used for that. This image - demonstrates the NetCat tool receiving log messages from Network target. -
-- NOTE: If your receiver application is ever likely to be off-line, don't use TCP protocol - or you'll get TCP timeouts and your application will be very slow. - Either switch to UDP transport or use AsyncWrapper target - so that your application threads will not be blocked by the timing-out connection attempts. -
-- There are two specialized versions of the Network target: Chainsaw - and NLogViewer which write to instances of Chainsaw log4j viewer - or NLogViewer application respectively. -
-${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- ${basedir}/${level}.log
- All
- Caution: Enabling this option can considerably slow down your file
- logging in multi-process scenarios. If only one process is going to
- be writing to the file, consider setting
- To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- The result is a message box: -
-- To set up the log target programmatically use code like this: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - This assumes just one target and a single rule. More configuration - options are described here. -
-- To set up the log target programmatically use code like this: -
-
- The example web service that works with this example is shown below
-
- - Asynchronous target wrapper allows the logger code to execute more quickly, by queueing - messages and processing them in a separate thread. You should wrap targets - that spend a non-trivial amount of time in their Write() method with asynchronous - target to speed up logging. -
-- Because asynchronous logging is quite a common scenario, NLog supports a - shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to - the <targets/> element in the configuration file. -
-
-
- ... your targets go here ...
-
- ]]>
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- ${longdate}|${level:uppercase=true}|${logger}|${message}
- This example causes the messages to be written to server1, - and if it fails, messages go to server2.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages not contains the string '1' to be ignored.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- - This example works like this. If there are no Warn,Error or Fatal messages in the buffer - only Info messages are written to the file, but if there are any warnings or errors, - the output includes detailed trace (levels >= Debug). You can plug in a different type - of buffering wrapper (such as ASPNetBufferingWrapper) to achieve different - functionality. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt - chosen randomly on a per-message basis. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each log message to be repeated 3 times.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes each write attempt to be repeated 3 times, - sleeping 1 second between attempts if first one fails.
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to either file1.txt or file2.txt. - Each odd message is written to file2.txt, each even message goes to file1.txt. -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- This example causes the messages to be written to both file1.txt or file2.txt -
-- To set up the target in the configuration file, - use the following syntax: -
-
- - The above examples assume just one target and a single rule. See below for - a programmatic configuration that's equivalent to the above config file: -
-
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- A flag to indicate whether an error should be thrown if no token is found.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- A flag to indicate whether an error should be thrown if no token is found.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- A flag to indicate whether an error should be thrown if no token is found.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- A flag to indicate whether an error should be thrown if no token is found.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- A flag to indicate whether an error should be thrown if no token is found.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- /*...*/
containing the specified text.
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- Tables[0].DefaultView[0].Price
in C# or
- Tables(0).DefaultView(0).Price
in Visual Basic.
-
- A flag to indicate whether an error should be thrown if no token is found.
- /*...*/
containing the specified text.
-