Correct.  Mocking only works if you control creation of the object.

---
Patrick Steele
http://weblogs.asp.net/psteele



On Thu, Nov 10, 2011 at 12:41 PM, Gavin van der Merwe
<[email protected]> wrote:
> I think Patrick is saying that mocking MyClass this way is not possible.
> You need to structure your class in a de-coupled way ...
> public class ClassThatDoesWork
> {
>    private MyClass instance;
>    public ClassThatDoesWork(MyClass instance)
>    {
>       this.instance = instance;
>    }
>    public void DoWork(someInputParameters)
>    {
>      // do stuff with mocked instance
>    }
> }
>
> On 10 November 2011 17:31, Laksh <[email protected]> wrote:
>>
>> Thanks, i'll would do that, and we are using Repository pattern, so
>> its easy change.
>>
>> i have a question though. In my scenario the method returns the
>> Package class so the caller of this method knows Package class.so i'm
>> able to Assert the values.
>> If i have a method which internally creates instance of class, would
>> it be possible to mock that class. see the example below
>>
>> public void DoWork(someInputParameters)
>> {
>>   MyClass instance = new MyClass()
>>   // do some logic here.
>>   // I  may use some .Net classes also to perform the logic
>> }
>>
>> is it possible to mock MyClass & .Net classes here ?
>>
>>
>> On Nov 10, 10:18 am, Patrick Steele <[email protected]> wrote:
>> > You want to make sure you have a loosely coupled architecture so you
>> > can do your unit tests in isolation.
>> >
>> > The Manifest class you originally showed looks like it needs to get
>> > data from a database (The GetData method).  That dependency on a
>> > database should be in a different component.  One idea is to create a
>> > repository interface for accessing the data:
>> >
>> > interface IDataObjectRepository
>> > {
>> >     DataObject GetData(IContext);
>> >
>> > }
>> >
>> > Your Manifest class now has a dependency on the IDataObjectRepository
>> > which can be mocked at test time using Rhino.Mocks.  This way you
>> > don't have to have an actual database up and running to execute your
>> > unit test for BuildManifest().  You don't have to worry about proper
>> > table structures and things like that.
>> >
>> > So iyour Manifest class now takes in the repository:
>> >
>> > public class Manifest : IManifest<Package>
>> > {
>> >     private IContext _context = null;
>> >         private IDataObjectReposutory _repository = null;
>> >
>> >         public Manifest(IContext context, IDataObjectReposutory
>> > repository)
>> >         {
>> >             _context = context;
>> >                 _repository = repository;
>> >         }
>> >
>> >         public Package BuildManifest()
>> >         {
>> >                 ...
>> >                 DataObject data = _repository.GetData(_context);
>> >                 ...
>> >         }
>> >
>> > }
>> >
>> > Now you can mock your context and repository and test to make sure
>> > that your Package gets built properly based on the data returned by
>> > IDataObjectRepository.GetData():
>> >
>> > // arrange
>> > var context = MockRepository.GenerateStub<IContext>();
>> > var repository = MockRepository.GenerateStub<IDataObjectRespository>();
>> > var do = new new DataObject { BatchID = "100", Title="Test" };
>> > repository.Stub(r => r.GetData(context)).Returns(do);
>> >
>> > // act
>> > var manifest = new Manifest(context, repository);
>> > var pacakgeManifest = manifest.BuildManifest();
>> >
>> > // assert
>> > Assert.AreEqual(do.BatchID, packageManifest.BatchID);
>> > Assert.AreEqual(do.Title, packageManifest.Title);
>> > etc...
>> >
>> > ---
>> > Patrick Steelehttp://weblogs.asp.net/psteele
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > On Thu, Nov 10, 2011 at 10:39 AM, Laksh <[email protected]> wrote:
>> > > GetData method  gets the data from the database and then I assign the
>> > > data to Package class hierarchy.
>> > > I'm creating the instance of Package inside the method. So how Rhino
>> > > will test such things. My question was not only for this method. In
>> > > general it is very common we create instances of custom classes, .Net
>> > > classes inside the method. (Basically local variables). In such case
>> > > how do test the method using Rhino?
>> >
>> > > On Nov 9, 10:33 am, Patrick Steele <[email protected]> wrote:
>> > >> What does GetData do?  You probably need to stub that out, but
>> > >> without
>> > >> knowing what it does, I can't say for sure.
>> >
>> > >> ---
>> > >> Patrick Steelehttp://weblogs.asp.net/psteele
>> >
>> > >> On Wed, Nov 9, 2011 at 11:20 AM, Laksh <[email protected]> wrote:
>> > >> > I have the following method. I'm creating a instance of Package
>> > >> > class
>> > >> > inside the method, setting some of its proeprties and then
>> > >> > returning
>> > >> > it. How do i unit test this method using Rhino?
>> >
>> > >> > public class Manifest : IManifest<Package>
>> > >> >    {
>> > >> >        private IContext _context = null;
>> >
>> > >> >        publicManifest(IContext context)
>> > >> >        {
>> >
>> > >> >            _context = context;
>> > >> >        }
>> >
>> > >> >        public Package BuildManifest()
>> > >> >        {
>> > >> >            // Data object is class which holds the data from
>> > >> > datasource.
>> > >> >            DataObject data = GetData(_context);
>> >
>> > >> >            Package package = new Package();
>> > >> >            package.BatchID = data.BatchId;
>> > >> >            package.Title = data.Title;
>> > >> >            package.Name = data.Name;
>> > >> >            package.City = data.City;
>> >
>> > >> >            IList<PackageDocument> documents = new
>> > >> > List<PackageDocument>();
>> > >> >            foreach (Document document in data.ConvertedDocuments)
>> > >> >            {
>> > >> >                PackageDocument packageDocument = new
>> > >> > PackageDocument();
>> > >> >                packageDocument.FileName = document.DocumentName;
>> > >> >                packageDocument.PrintOrder =
>> > >> > document.SequenceNumber.Value;
>> > >> >                packageDocument.PaperType =
>> > >> > PackageDocumentPaperType.White;
>> > >> >                documents.Add(packageDocument);
>> > >> >            }
>> > >> >            package.Documents = documents.ToArray();
>> > >> >            return package;
>> > >> >        }
>> > >> >    }
>> >
>> > >> > --
>> > >> > 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
>> > >> > athttp://groups.google.com/group/rhinomocks?hl=en.
>> >
>> > > --
>> > > 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
>> > > athttp://groups.google.com/group/rhinomocks?hl=en.
>>
>> --
>> 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.
>>
>
> --
> 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.
>

-- 
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