Hi Josef,

as Patrick already mentioned, the simplest solution would be to inject
the DataGenerator into the DataConsumer:

         public class DataConsumer
         {
                 private readonly DataGenerator dg;

                 public DataConsumer (DataGenerator dataGenerator)
                 {
                         dg = dataGenerator;
                 }

                 public int GetNameLength (  )
                 {
                         return dg.GetName().Length;
                 }
         }

To be able to mock the DataGenerator with Rhino Mocks you also have to
introduce an interface for the DataGenerator or at least  make the
method GetName() 'virtual':

         public class DataGenerator
         {
                 public virtual string GetName()
                 {
                         return "My Name";
                 }
         }

You will have a little more code, but a much better design (less
coupling). You get the possibility to test your DataConsumer
thoroughly, which can include test for error cases (E.g.: How does the
DataConsumer behave, when the DataGenerator throws an exception?)

Kind Regards
Sven

On 20 Mrz., 15:10, Josef Semmler <[email protected]> wrote:
> Hi there,
>
> i'm new to rhino and new to mocking - so sorry for propably asking a
> stupid question:
>
> I have code which looks something like this (simplified):
>
>         public class DataGenerator
>         {
>                 public string GetName()
>                 {
>                         return "My Name";
>                 }
>         }
>
>         public class DataConsumer
>         {
>                 public DataConsumer () {}
>
>                 public int GetNameLength (  )
>                 {
>                         DataGenerator dg = new DataGenerator();
>                         return dg.GetName().Length;
>                 }
>         }
>
> I'd like to create a Unit Test for the "DataConsumer".  I'm wondering
> if it is possible with Rhino.Mocks to get the "DataGenerator" mocked
> somehow - without changing the code (in particular, without making the
> local "dg" variable a class variable).
>
> Thanks for any thoughts!
> JS

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
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.

Reply via email to