I've hit an impasse with Machine.Fakes. I cannot figure out how to mock an out
parameter using only Machine.Fakes equipment. Because of a bug in RhinoMocks, I switched our mfakes adapter to FakeItEasy. As far as I can tell, any of the adapters should be interchangable.
The problem is that this caused the "out" tests to fail, things that looked like this no longer compile, because Arg
was Rhino.Mocks.
The<IMembershipService>()
.WhenToldTo(x => x.CreateUser(Param<string>.IsAnything,
Param<bool>.IsAnything,
Param<object>.IsAnything,
out Arg<MembershipCreateStatus>
.Out(MembershipCreateStatus.UserRejected)
.Dummy))
.Return(user);
I tried using a "dummy" local variable, set to the same value the original Arg<T>
param set it to, but this has caused my tests to fail -- it seems as though the value isn't being passed through! Arg<T>
really had the solution, but I can't use it anymore, as it's part of Rhino.Mocks.
Since version 1.7.0 Machine.Fakes supports setting up out
and ref
parameters in fake calls - when using the FakeItEasy or NSubstitute adapters. Thus, you don't have to use FakeItEasy directly any more. Alex' example can be simplified like this:
using Machine.Fakes;
using Machine.Specifications;
namespace MSpecMFakesOutParam
{
public interface IFoo
{
void Foo(out int foo);
}
public class When_using_FakeItEasy_with_out_params : WithFakes
{
static int Out;
Establish context = () =>
{
int ignored;
The<IFoo>().WhenToldTo(x => x.Foo(out ignored)).AssignOutAndRefParameters(42);
};
Because of = () => The<IFoo>().Foo(out Out);
It should_assign_the_out_param = () => Out.ShouldEqual(42);
}
}