Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock an out parameter with moq or rhino mock or something else

I tried with NMock2 but I get TypeLoadExceptions when trying to pass the mocks into the constructor, also I saw TypeMock can do that but it costs 80$

like image 699
Omu Avatar asked Dec 10 '09 13:12

Omu


2 Answers

I found out myself, you can actually do that with Moq, it's like this:

var info = new Info { stuff = 1 };

textReader.Setup(o => o.Read<CandidateCsv>("", out info));

that's it :)

like image 138
Omu Avatar answered Nov 10 '22 04:11

Omu


Moq does not support mocking out/ref parameters, but you can do it using Rhino Mocks using OutRef, which accepts one argument for each out/ref parameter in the method.

MockRepository mockRepository = new MockRepository();

// IService.Execute(out int result);
var mock = mockRepository.CreateStub<IService>();

int mockResult; // Still needed in order for Execute to compile

mock.Setup(x => x.Execute(out mockResult)).OutRef(5);
mock.Replay();

int result;

mock.Execute(out result);

Assert.AreEqual(5, result);
like image 3
Richard Szalay Avatar answered Nov 10 '22 06:11

Richard Szalay