Thank you for providing this information. We were planning to use the Rhino
Mocks for legacy code that we cannot change and now I understood we have to
find different ways to test that legacy code.
Again, thank you for your help on this topic.
On Friday, June 15, 2012 8:44:41 AM UTC-4, Patrick Steele wrote:
>
> 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
>
>
> On Fri, Jun 15, 2012 at 1:16 AM, alwaysgo <[email protected]>
> wrote:
> > Hi,
> >
> > I need help figuring out how I can change the method call inside of
> another
> > method.
> >
> > For example:
> >
> > public class A
> > {
> > public string getValue()
> > {
> > string retVal = "";
> > B b = new B();
> >
> > retVal = b.getB();
> > retVal = retVal + " Final ";
> > return retVal;
> > }
> > }
> >
> > public class B
> > {
> > public string getB()
> > {
> > return "B";
> > }
> > }
> >
> > public class BStub
> > {
> > public string getBStub()
> > {
> > return "BStub";
> > }
> > }
> >
> > public class TestDriver
> > {
> > public static void Main()
> > {
> > A aClass = MockRepository.GenerateMock<A>();
> > B bClass = MockRepository.GenerateStub<B>();
> >
> > bClass.Stub(x=>x.getB()).Return(new BStub().getBStub())
> > .Do(new Func<string>(()=>new
> > BStub().getBStub()));
> >
> >
> > string result = aClass.getValue(); //I am not getting any value
> to
> > result
> > //In this case,
> I
> > want to get "BStub Final" after calling
> > // getValue()
> > method on 'aClass'
> >
> > }
> >
> > }
> >
> > Any help would be appreciated.
> >
> > Thanks
> >
> >
> > --
> > 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 [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/rhinomocks?hl=en.
>
On Friday, June 15, 2012 8:44:41 AM UTC-4, Patrick Steele wrote:
>
> 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
>
>
> On Fri, Jun 15, 2012 at 1:16 AM, alwaysgo <[email protected]>
> wrote:
> > Hi,
> >
> > I need help figuring out how I can change the method call inside of
> another
> > method.
> >
> > For example:
> >
> > public class A
> > {
> > public string getValue()
> > {
> > string retVal = "";
> > B b = new B();
> >
> > retVal = b.getB();
> > retVal = retVal + " Final ";
> > return retVal;
> > }
> > }
> >
> > public class B
> > {
> > public string getB()
> > {
> > return "B";
> > }
> > }
> >
> > public class BStub
> > {
> > public string getBStub()
> > {
> > return "BStub";
> > }
> > }
> >
> > public class TestDriver
> > {
> > public static void Main()
> > {
> > A aClass = MockRepository.GenerateMock<A>();
> > B bClass = MockRepository.GenerateStub<B>();
> >
> > bClass.Stub(x=>x.getB()).Return(new BStub().getBStub())
> > .Do(new Func<string>(()=>new
> > BStub().getBStub()));
> >
> >
> > string result = aClass.getValue(); //I am not getting any value
> to
> > result
> > //In this case,
> I
> > want to get "BStub Final" after calling
> > // getValue()
> > method on 'aClass'
> >
> > }
> >
> > }
> >
> > Any help would be appreciated.
> >
> > Thanks
> >
> >
> > --
> > 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 [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/rhinomocks?hl=en.
>
On Friday, June 15, 2012 8:44:41 AM UTC-4, Patrick Steele wrote:
>
> 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
>
>
> On Fri, Jun 15, 2012 at 1:16 AM, alwaysgo <[email protected]>
> wrote:
> > Hi,
> >
> > I need help figuring out how I can change the method call inside of
> another
> > method.
> >
> > For example:
> >
> > public class A
> > {
> > public string getValue()
> > {
> > string retVal = "";
> > B b = new B();
> >
> > retVal = b.getB();
> > retVal = retVal + " Final ";
> > return retVal;
> > }
> > }
> >
> > public class B
> > {
> > public string getB()
> > {
> > return "B";
> > }
> > }
> >
> > public class BStub
> > {
> > public string getBStub()
> > {
> > return "BStub";
> > }
> > }
> >
> > public class TestDriver
> > {
> > public static void Main()
> > {
> > A aClass = MockRepository.GenerateMock<A>();
> > B bClass = MockRepository.GenerateStub<B>();
> >
> > bClass.Stub(x=>x.getB()).Return(new BStub().getBStub())
> > .Do(new Func<string>(()=>new
> > BStub().getBStub()));
> >
> >
> > string result = aClass.getValue(); //I am not getting any value
> to
> > result
> > //In this case,
> I
> > want to get "BStub Final" after calling
> > // getValue()
> > method on 'aClass'
> >
> > }
> >
> > }
> >
> > Any help would be appreciated.
> >
> > Thanks
> >
> >
> > --
> > 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 [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/rhinomocks?hl=en.
>
--
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/-/alB8-L1LukIJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhinomocks?hl=en.