Rhino.Mocks和ref参数 [英] Rhino.Mocks and ref parameter
问题描述
我有问题来测试,有一个ref参数的方法。我不是库/代码老板,所以我不能改变它,所以请不要认为我删除ref参数。
I'm having problems to test a method that has a ref parameter. I'm not the library/code owner, so I can't change it, so please do not suggest that I remove the ref parameter.
我用这网站作为参考:
http://ayende.com/wiki/Rhino %20Mocks%203.5.ashx#OutandRefarguments
I'm using this website as reference: http://ayende.com/wiki/Rhino%20Mocks%203.5.ashx#OutandRefarguments
下面是测试:
[Test]
public void TestBuildSimpleProfile()
{
// arrange
var barMock = MockRepository.GenerateStrictMock<ICommandBar>();
var controlBuilder = new ControlBuilder(barMock);
var user = new Usuario();
user.PRF_PERFIS = new Perfis();
var perfil = new Perfil();
perfil.FNC_FUNCIONALIDADES = new Funcionalidades();
var func1 = new Funcionalidade();
func1.FNC_NU_ID = 1;
func1.FNC_FL_ATIVO = true;
func1.FNC_NO_CHAVE = "Associar Registros";
func1.FNC_DE_FUNCIONALIDADE = "{0653aeac-c5ef-46fa-9e99-408719296ed3}";
perfil.FNC_FUNCIONALIDADES.Add(func1);
user.PRF_PERFIS.Add(perfil);
var funcs = new List<IFunctionality>();
funcs.Add(new FunctionalityAttribute(1,"Associar Registros", "{0653aeac-c5ef-46fa-9e99-408719296ed3}", "SGIGT", true,"admin,editor"));
var uid = new UIDClass() { Value = "{0653aeac-c5ef-46fa-9e99-408719296ed3}" };
var arg = Arg<object>.Ref(Is.Anything(), 0).Dummy;
// act
controlBuilder.Build(user, funcs);
// assert
barMock.AssertWasCalled(x => x.Add(uid, ref arg), y => y.IgnoreArguments());
}
下面是我发现的错误:
Rhino.Mocks.Exceptions.ExpectationViolationException:ICommandBar.Add(ESRI.ArcGIS.esriSystem.UIDClass,0);预计#0,实际#1。
Rhino.Mocks.Exceptions.ExpectationViolationException : ICommandBar.Add(ESRI.ArcGIS.esriSystem.UIDClass, 0); Expected #0, Actual #1.
我已经尝试了多种方法,但它根本不起作用。构建方法实现如下。这只是接收功能来构建和一个用户,这已关联的功能列表。如果他有一个功能,它应该是建立,使用add方法
I've already tried a number of ways, but it simply does not work. The build method implementation is below. This just receives a list of functions to build and an user, which has associated functions. If he has a function, it should be build, using the add method.
public void Build(Usuario u, List<IFunctionality> funcsToBuild)
{
if (u == null)
throw new ArgumentNullException("u");
if (funcsToBuild == null)
throw new ArgumentNullException("funcsToBuild");
if (u.PRF_PERFIS == null || u.PRF_PERFIS.Count <= 0)
return;
var functionList = (from funcs in funcsToBuild
select funcs.FunctionDescription).ToList();
var userFuncs = (from profile in u.PRF_PERFIS
from functionality in profile.FNC_FUNCIONALIDADES
where functionality.FNC_FL_ATIVO.HasValue && functionality.FNC_FL_ATIVO.Value && functionList.Contains(functionality.FNC_DE_FUNCIONALIDADE)
select new FunctionalityAttribute((int)functionality.FNC_NU_ID.Value,functionality.FNC_NO_CHAVE, functionality.FNC_DE_FUNCIONALIDADE, "SGIGT", true, "")).Cast<IFunctionality>().OrderBy(x => x.FunctionId).ToList();
for (int index = 0; index < userFuncs.Count; index++)
{
IFunctionality t = userFuncs[index];
object i = index;
var functionality = t;
var uid = new UIDClass() {Value = functionality.FunctionDescription};
var ci = _commandBar.Add(uid, ref i);
}
}
任何机会,有人能帮助我吗?
Any chance someone can help me?
推荐答案
您可以使用精氨酸和LT;>匹配上出/ ref参数的期望。如果你不关心实际值,可以使用类似以下内容:
You can use Arg<> to match on expectations for Out/Ref parameters. If you don't care about the actual value, you could use something like the following:
barMock.AssertWasCalled(x =>
x.Add(Arg<UIDClass>.IsAnything(),
ref Arg<object>.Ref(Is.Anything(), null).Dummy);
如果你真的关心匹配的输入/输出值,那么你可以用这个来代替:
If you do care about matching input/output values, then you could use this instead:
barMock.AssertWasCalled(x =>
x.Add(Arg<UIDClass>.Is.Equal(uid),
ref Arg<object>.Ref(Is.Same(input), output).Dummy);
我觉得你的测试最有可能失败,因为流体是不一样的实例作为一个传递到ICommandBar,而不是与参考参数的一个具体问题,你可以通过使用equals如上图所示,而不是使用引用相等解决这个问题。
I think your test is most likely failing because uid is not the same instance as the one passed into ICommandBar, rather than a specific issue with the Ref parameter. You can solve this by using Equals as shown above, rather than using reference equality.
如果一切都失败了,并UIDClass不执行等于正确,你可以随时使用匹配谓词:
If all else fails and UIDClass doesn't implement equals correctly, you can always use a matching predicate:
barMock.AssertWasCalled(x =>
x.Add(Arg<UIDClass>.Is.Matching(
delegate(UIDClass u) { return u.Value == uid.Value; }
),
ref Arg<object>.Ref(Is.Same(input), output).Dummy);
这篇关于Rhino.Mocks和ref参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!