You can't. At least not with Rhino.Mocks.
This really leads to a discussion on Inversion of Control (IOC) and
Dependency Injection (DI). What you have now is a hard dependency on
a new "B" inside A.getValue(). This makes maintenance and mods harder
in the long run. What I would suggest, and this would lead to more
testable code, is to pass in a factory interface to class A's
constructor. That factory will be responsible for creating B. By
doing it this way, you could inject a stub "B" by injecting a stub
factory. Something like this:
public interface IBeeFactory
{
B Create();
}
public class A
{
private readonly IBeeFactory bFactory;
public A(IBeeFactory bFactory)
{
this.bFactory = bFactory;
}
public string getValue()
{
string retVal = "";
B b = bFactory.Create();
retVal = b.getB();
retVal = retVal + " Final ";
return retVal;
}
}
Now your test can be something like this:
var factory = MockRepository.GenerateStub<IBeeFactory>();
factory.Stub(f => f.Create()).Return(new B());
var a = new A(factory);
Assert.AreEqual("BStubFinal", a.getValue());
NOTE: Just banged the code out quickly. Be wary of errors.
---
Patrick Steele
http://weblogs.asp.net/psteele
> --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/rhinomocks/-/lvq3GMbEl9cJ.
> To post to this group, send email to
rhino...@googlegroups.com.
> To unsubscribe from this group, send email to
>
rhinomocks+...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/rhinomocks?hl=en.