C# Rhino Mocks-使用ref/out参数模拟集合
我还在学习犀牛模仿,对此我有一个疑问。例如,我在模拟界面中有一个函数:C# Rhino Mocks-使用ref/out参数模拟集合,c#,rhino-mocks,C#,Rhino Mocks,我还在学习犀牛模仿,对此我有一个疑问。例如,我在模拟界面中有一个函数: public interface ISomeObject { string Name {get; set;} int Id {get;set;} } // This class will be returned as and answer to function call public class AnswerObject { pub
public interface ISomeObject
{
string Name {get; set;}
int Id {get;set;}
}
// This class will be returned as and answer to function call
public class AnswerObject
{
public bool IfError {get;set;}
}
// Main interface
public interface IClass
{
AnswerObject FunctionGetCollection(ref ICollection <ISomeObject> ListOfInternalObjects, ref int Number);
}
公共接口对象
{
字符串名称{get;set;}
int Id{get;set;}
}
//该类将作为函数调用的响应返回
公共类应答对象
{
公共bool IfError{get;set;}
}
//主界面
公共接口类
{
AnswerObject函数GetCollection(内部对象的引用ICollection列表,引用整数编号);
}
如您所见,函数'FunctionGetCollection'将接收作为'ref'传递的2个参数,并返回另一个类作为'function answer'。你能帮我删除这个函数吗?我需要能够使用:
- 函数将返回不同的集合(基于代码中的就地集合,而不是基于参数)
- 函数将返回不同的AnswerObject
- 语法不是很好。它不经常使用,并且使用老式的
Rhino.Mocks.Constraints
这段代码建立了一个模拟,用新值替换所有ref参数
AnswerObject answerObject;
ICollection <ISomeObject> collection;
int number;
IClass iClassMock = MockRepository.GenerateMock<IClass>();
iClassMock
.Stub(x => x.FunctionGetCollection(
ref Arg<ICollection <ISomeObject>>.Ref(Is.Anything(), collection).Dummy,
ref Arg<int>.Ref(Is.Anything(), number).Dummy);
.Return(answerObject);
AnswerObject AnswerObject;
i收集;
整数;
IClass iClassMock=MockRepository.GenerateMock();
iClassMock
.Stub(x=>x.FunctionGetCollection(
ref Arg.ref(Is.Anything(),collection).Dummy,
ref Arg.ref(Is.Anything(),number.Dummy);
.返回(应答对象);
如果希望在将值传递到模拟时保留这些值,则需要在WhenCalled块中实现这一点:
iClassMock
.Stub(x => x.FunctionGetCollection(
ref Arg<ICollection <ISomeObject>>.Ref(Is.Anything(), null).Dummy,
ref Arg<int>.Ref(Is.Anything(), 0).Dummy);
.WhennCalled(call =>
{
// reset the ref arguments to what had been passed to the mock
// not sure if it also works with the int
call.Arguments[0] = call.Arguments[0];
call.Arguments[1] = call.Arguments[1];
})
.Return(answerObject);
iClassMock
.Stub(x=>x.FunctionGetCollection(
ref Arg.ref(Is.Anything(),null).Dummy,
ref Arg.ref(Is.Anything(),0.Dummy);
.whenCalled(call=>
{
//将ref参数重置为已传递给mock的参数
//不确定它是否也适用于int
call.Arguments[0]=call.Arguments[0];
call.Arguments[1]=call.Arguments[1];
})
.返回(应答对象);
Is.Anything-给出一个错误:参数1:无法从“方法组”转换为“Rhino.Mocks.Constraints.AbstractConstraint”。(我还根据函数要求在Arg参数之前添加了“ref”):(:(这是正确答案-在两个位置都使用Is.Anything(),在“Arg”之前添加“ref”就行了!反正我已经接近了:):)