如何使用Moq框架根据输入参数设置参数 [英] How to use Moq framework to setup out argument based on input argument

查看:61
本文介绍了如何使用Moq框架根据输入参数设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我正在使用.NET 4.6.2和Moq框架4.2.1510.2205(运行时版本v4.0.20926)。我在设置我的接口的mock方法时遇到了麻烦,该方法有第一个int参数和第二个输出字符串参数,应该根据第一个参数的值设置。
这是我的代码:

I'm using .NET 4.6.2 and Moq framework 4.2.1510.2205 (runtime version v4.0.20926). I'm having a trouble to setup mock method of my interface that has first int parameter and second out string parameter that should be set based on value of first parameter. Here is my code:

using Moq;
using System;
using System.Collections.Generic;

namespace TestMoqFramework
{
	public interface IFoo
	{
		bool TryGetStringValue(int stringID, out string stringValue);
	}

	class Program
	{
		static void Main(string[] args)
		{
			Dictionary<int, string> mockValues = new Dictionary<int, string> { { 1, "1" }, { 2, "2" }, { 3, "3" } };
			Mock<IFoo> fooMock = new Mock<IFoo>(MockBehavior.Strict);
			string outStringSetup = "This is initial value";
			fooMock.Setup(foo => foo.TryGetStringValue(It.IsAny<int>(), out outStringSetup)).Returns<int, string>((intValue, outStringReturn) =>
			{
				if (mockValues.TryGetValue(intValue, out outStringReturn))
				{
					outStringSetup = outStringReturn;
					Console.WriteLine("Inside mock: successfully called for int value {0}. String \"{1}\" should be returned.", intValue, outStringSetup);
					return true;
				}
				else
				{
					Console.WriteLine("Inside mock: Didn't find value {0} in dictionary", intValue);
					outStringReturn = "Failed to set string in mock";
					outStringSetup = outStringReturn;
					return false;
				}
			});

			IFoo fooImplementation = fooMock.Object;
			int intVal = 1;
			string stringToBeSet = "This string is yet to be set";
			if (fooImplementation.TryGetStringValue(intVal, out stringToBeSet))
			{
				Console.WriteLine("Outside mock: Success! String value of int {0} is \"{1}\".", intVal, stringToBeSet);
			}
			else
			{
				Console.WriteLine("Outside mock: Failed!");
			}
			Console.ReadKey();
		}
	}
}

控制台输出如下:

里面mock:成功调用int值1.字符串"1"应该退还。

外面模拟:成功! int 1的字符串值是"This is initial value"。

Inside mock: successfully called for int value 1. String "1" should be returned.
Outside mock: Success! String value of int 1 is "This is initial value".

正如您所看到的,调用了 TryGetStringValue  方法的模拟实现,但是在  stringToBeSet 变量的结束值是  "这是初始值"
而不是"1"正如我所料。有谁知道为什么程序以这种方式表现?我的问题有解决方案吗?

As you can see, mock implementation of TryGetStringValue method was called, but at the end value of stringToBeSet variable is "This is initial value" and not "1" as I expected. Does anyone know why program is behaving in this way? Is there a solution to my problem?

提前谢谢。

推荐答案

我不确定什么设置和返回中的参数之间的区别( outStringSetup
和  outStringReturn )所以我将值设置为两个参数以防万一,但它没有帮助将  stringToBeSet
的值设置为" 1"。
I'm not sure whats the difference between arguments in setup and returns (outStringSetup and outStringReturn) so I set values to both arguments just in case, but it didn't help to set value of stringToBeSet to "1".


这篇关于如何使用Moq框架根据输入参数设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆