5

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$

TrueWill
  • 25,132
  • 10
  • 101
  • 150
Omu
  • 69,856
  • 92
  • 277
  • 407

2 Answers2

7

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 :)

Omu
  • 69,856
  • 92
  • 277
  • 407
3

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);
Community
  • 1
  • 1
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • 3
    This is no longer correct. Moq does support out/ref parameters. See http://code.google.com/p/moq/wiki/QuickStart – TrueWill Dec 08 '10 at 20:15
  • @TrueWill - The level of support has not changed. You still cannot expect a method call with a certain `ref` argument and then specify that that argument be changed to another value. – Richard Szalay Dec 08 '10 at 22:04
  • true, but you can simply declare a new mock. A limitation on what can be changed after setup is not the same thing as lack of support for mocking out/ref parameters. – TrueWill Dec 09 '10 at 16:02
  • @TrueWill - I disagree, the aforementioned scenario is not possible making `ref` parameters, for all intents an purposes, unsupported. `out` parameters are supported, though, granted. – Richard Szalay Dec 10 '10 at 08:25
  • -1 for the fact which is stated in Chuck Norris. I use that code often. – radu florescu May 30 '12 at 06:34
  • @Floradu88 - the parameters cannot be mocked. Fot example, you cannot change the value of a ref parameter based on it's initial value. – Richard Szalay May 30 '12 at 22:19
  • Yeah but the value you need is the out value which you can set. That if the whole purpose of out. – radu florescu May 31 '12 at 05:52