In general, if you go with a factory method, which produces Customer objects out of the database data, and do the validation there, instead in the constructor, you will have much more testable code. I would not put database access in a ctor anyway.
They you can test this factory method by stubbing only the database access the way Patrick shows. Cheers On Thu, Feb 24, 2011 at 12:59 PM, Brett <[email protected]> wrote: > I should have been more specific in my example. The Load actually > hydrates the customer, not a theme. Copy/paste error. > > public Customer(int customerID) { ... > ...validation... > customerDataAccess.Load(this, customerID); > } > > so then, after something like: > > Customer c = new Customer(123); > > I could then access c.BillingAddress, c.BalanceDue, whatever else, > etc... > > > > > On Feb 24, 11:21 am, Brett <[email protected]> wrote: >> I have a scenario that I want to mock and unit test, but I'm not sure >> of the best approach. I have a class with a constructor that takes an >> ID, which then does some validation and hydrates itself from a >> mockable data access class. Customer would be an example class name: >> >> public Customer(int customerID) { ... >> ...validation... >> customerDataAccess.LoadTheme(this, customerID); >> >> } >> >> so that you could use this class like: >> >> Customer c = new Customer(123); >> >> The problem is with passing "this" to the mocked method. At that >> point, the mock of ICustomerDataAccess does not update the current >> instance. Under the hood, CustomerDataAccess uses a mockable >> IDataAccess, so I could use an actual CustomerDataAccess instance and >> instead mock IDataAccess. That's fine, but I was wondering if anybody >> has worked with a scenario like this in the past. I think I could use >> WhenCalled and set properties of the instance that is created, but I'm >> not sure that is really what I want to do. >> >> Thanks. > > -- > 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. > > -- Svetoslav Milenov (Sunny) Artificial Intelligence is no match for natural stupidity. -- 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.
