amateur
amateur

Reputation: 44605

assistance with testing abstract method with rhino mocks

I am looking to write some unit tests for my c# class. I have a base class called BaseClass that has an abstract method called Execute and also a method called Redirect. I have a class called Class1 which inherits BaseClass and implements the abstracted Execute method, the method I want to test. The following code explains the set up further:

public abstract class BaseClass
{
    public abstract void Execute();

    public void Redirect()
    {
        // redirect code here
    }
}

public class Class1 : BaseClass
{
    public void Execute()
    {
      // do some processing

      this.Redirect();
    }
}

I am working with mstest and using rhino mocks for my mocking. I want to write tests for the Execute method to test it works as I expect.

As you can see from above, the Execute method makes a call to the base method Redirect, so I have an expectation that the Redirect method is called.

I use the Rhino mocks mockrepository to create a Partial Mock of Class1. The created mock contains the Execute method which is great, but does not contain a reference to the Redirect method, which is in Class1. I want to be able to set an expectation on the mocked repository that the Redirect method is called.

Any tips or advice as how I might be able to create a test with rhino mocks to achieve what I have outlined?

Upvotes: 0

Views: 977

Answers (2)

Lunivore
Lunivore

Reputation: 17602

If you need to mock out that method, it's likely that the behavior is complicated enough that you can't test it directly.

For that reason, please consider delegating that behavior to a collaborating class, then mocking that class out entirely.

For instance, if the execute method was saving an entity to a database, I'd create a repository, dependency-inject that repository through the constructor, then ask the repository to save my entity. The entire collaborating class can then be mocked out using standard techniques.

At the moment it seems as if your class is responsible for too much, which is why you're having problems. Mocks work very effectively when they're used to express collaboration, which allows us to create classes with single responsibilities, which allow us to create well-designed, maintainable code. I recommend Steve Freeman and Nat Pryce's book on the subject.

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160882

You will have to declare the base class method Redirect() as virtual so Rhino Mocks can mock it. The following worked for me:

public abstract class BaseClass
{
    public abstract void Execute();

    public virtual void Redirect()
    {
        // redirect code here
    }
}

public class Class1 : BaseClass
{
    public override void Execute()
    {
        // do some processing

        this.Redirect();
    }
}

..
var class1Mock = Rhino.Mocks.MockRepository.GeneratePartialMock<Class1>();

class1Mock.Expect(x => x.Redirect());
class1Mock.Execute();
class1Mock.VerifyAllExpectations();

Upvotes: 1

Related Questions