问题
I am stubbing the return value of a method using rhino mocks. However, I want to return the same dummy value for any argument that is passed in.
How do I do this without pre-registering every input to return the same output?
回答1:
_testHelper is helper class where you are returning a dummy value from GetMethodValue(). you have to write GetMethodValue() in your _testHelper class.
SetupResult.For(_Repository.MethodName(null)).IgnoreArguments().Return(_testHelper.GetMethodNameResultValue());
回答2:
You'd use MyClass.Expect(x=>x.MyMethod(someArg)).Return(stubValue).IgnoreArguments()
回答3:
You can use IgnoreArguments()
constraint as shown below:
mockedInstance.Expect(instance => instance.MethodCall(null))
.IgnoreArguments()
.Return(preDefinedValue)
.Repeat()
.Any();
Also by specifying Repeat().Any()
preDefinedValue
will be returned for each call of a method.
See Rhino Mocks wiki for more examples.
来源:https://stackoverflow.com/questions/7498995/stub-return-value-for-all-inputs-in-rhino-mocks