2

I'm using rhino mock in my unit tests and I'm trying to create a mock using the following code:

var userDetails = MockRepository.GenerateMock<ReadOnlyCollection<UserDetails>>();

But when I run the unit test I get the following error:

Can not instantiate proxy of class: 
System.Collections.ObjectModel.ReadOnlyCollection`
1[[SolutionName.FolderName,]].
Could not find a parameterless constructor.

I have searched on the net and found similar questions and solutions, one for moq using the SetupGet() method but I don't know what the equivalent of this is in rhino mocks. (The UserDetails class does have a parameterless constructor) How do I create a stub/mock for the ReadOnlyCollection?

Theomax
  • 6,584
  • 15
  • 52
  • 72
  • 3
    Why do you need to mock it? I think I'd create an instance and set it up as I need. Don't see much to win with a mock. – Johan Larsson Sep 29 '13 at 18:44

2 Answers2

2

You can pass any constructor arguments to GenerateMock:

var inner = new List<UserDetails>();
var userDetails = MockRepository.GenerateMock<ReadOnlyCollection<UserDetails>>(inner);

You may want to consider creating an instance of ReadOnlyCollection in your test and returning it from some other method call, which will be much simpler than mocking the appropriate methods.

Lee
  • 142,018
  • 20
  • 234
  • 287
  • It got passed the original error, but now when I try and do anything with it I get 'Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).' - should I make the method I'm calling virtual just so rhino mocks can use it? Which I've just realised it's a static class so I can't lol – Theomax Sep 29 '13 at 18:55
  • @Theomax - It's hard to tell without seeing the code, but looking at `ReadOnlyCollection`, most of the methods are non-virtual so you can't mock them using rhino mocks. If you're using .Net 4.5 you could use `IReadonlyCollection` instead, however I would construct any test data you need in your test instead of trying to mock framework classes. – Lee Sep 29 '13 at 19:04
1

Not sure you can do this with Rhino Mock. The issue is that ReadOnlyCollection is not an interface, nor does it contain virtual methods, which open source mocking frameworks can work with.

Since ReadOnlyCollection implements IList you could try the suggested method found in this SO question

How to mock a private readonly IList<T> property using moq

Basically use an IList collection as a public property, but make the underlying list private, so you still get that read-only experience. Thus in your tests, you can use:

var userDetails = MockRepository.GenerateMock<IList<UserDetails>>();

If you really want to mock a ReadOnlyCollection, the you will need to buy either TypeMock or JustMock. Alternatively, get Visual Studio 2012 Premium with Update 2, where you can use the MS Fakes mocking framework.

EDIT: Lee's answer is much more efficient then mine :) I would suggest that one instead.

Community
  • 1
  • 1
Jason Evans
  • 28,906
  • 14
  • 90
  • 154