Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock protected virtual members with Rhino.Mocks?

Moq allows developers to mock protected members. I was looking for the same functionality in Rhino.Mocks but fail to find it.

Here's an example from Moq Quick Start page how to mock protected method.

// at the top of the test fixture
using Moq.Protected()

// in the test
var mock = new Mock<CommandBase>();
mock.Protected()
     .Setup<int>("Execute")
     .Returns(5);

// if you need argument matching, you MUST use ItExpr rather than It
// planning on improving this for vNext
mock.Protected()
    .Setup<string>("Execute",
        ItExpr.IsAny<string>())
    .Returns(true);

Let me know if I'm chasing something that doesn't exit.

like image 880
Vadim Avatar asked Apr 05 '10 20:04

Vadim


1 Answers

I believe this functionality does not exist in Rhino Mocks.

Why are you trying to mock protected members? Why not just test the class as a whole? Alternatively you can create a subclass of your test class and create "mocked" protected methods manually.

like image 57
Grzenio Avatar answered Sep 26 '22 13:09

Grzenio