.

Unit test protected methods using Rhino mocks

If your method is in a different assembly then you need to allow access to your own assembly (e.g MyAssembly) and e.g. Rhino mocks () or whatever you use for mocking

you can do so:

[assembly: InternalsVisibleTo("MyAssembly")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

then your unit test would look something like this:

[TestMethod]
public void TestMethod1()
{
    var txt = "testdata";
    var rawRequestProcessor = MockRepository.GenerateStub<RawRequestProcessor>();
    rawRequestProcessor.Process(txt);
    Assert.AreEqual(txt, rawRequestProcessor.RawInput);
    rawRequestProcessor.Expect(m => m.processInput());
    rawRequestProcessor.VerifyAllExpectations();
}

example of the code being tested here:

public class RawRequestProcessor
{
    public void Process(string txt)
    {
        RawInput = txt;
    }

    private string _rawInput;
    public string RawInput {
        get { return _rawInput; }
        set
        {
            _rawInput = value;
            processInput();
        }
    }

    internal virtual void processInput()
    {
            
    }
}

 

 

What's your thoughts on this?

*

Protected by WP Anti Spam