Hi, This is precisely why "new is considered bad" and why dependency injection was invented. :)
Btw, if you pass in a CityManager but then don't use it, creating your own instead, it isn't really dependency injection, is it? There are other patterns too of course.... Inject a factory of some sort which is responsible for creating the CityManager on demand, then mock the factory. Or move the CityManager creation to a virtual method, and have the test use a TestPersonManager that inherits from PersonManager and overrides the virtual method. Or get the CityManager from a static method, the static method will call into a factory, which it finds in a static property, and which the tests and bootstrap code can initialize to either a mock provider, or the real thing, respectively. Many of these methods come in handy when you try to take control of, refactor and create tests for a large legacy code base, where you are limited in how you can change the code. For new code, dependency injection is often the way to go. /Oskar 2012/1/15 Sruli Ganor <[email protected]>: > Hi, > > I'm testing a class PersonManager, which instantiates an object of > type CityManager with 'using'. > I want to mock all the CityManager methods and constructors. I.e. I > want to skip the constructor in 'using', the Save and Move methods > (which are public virtual). > While I can mock the other personmanager methods (e.g. > GetPersonByName), I found no way to mock the CityManager. > I tried also dependency injection and passed a CityManager object in > the PersonManager constructor, but this did not work either, probably > because the Using instantiates a new CityManager object. > > Is there any way to do it? > > Thanks in advance. > > > Public class PersonManager > { > public string GetFirstName(string lastName) > { > using (CityManager cityMgr = new CityManager("London")) > { > cityMgr.Move("UK"); > cityMgr.Save(); > } > > GetPersonByName(…); > } > } > > > > [TestMethod] > : > PersonManager personMgr = mocks.Stub<PersonManager>(BAcontext); > SetupResult.For(personMgr.GetPersonByLastName("Miller")).Return(fakePersons[0]); > name = personMgr.GetFirstName("Miller"); > Assert.AreEqual(mCity, "Joseph"); > > -- > 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.
