Simon: No, RM has no facility to generate random test values (unless of course you were to set an expectation that "when X is called, return Random.Next()" and yes, you're doing it right.
The reason you want to "bother" with mocking in the first place is so that consumers of your service can code to the interface (IAccountService) and then in production call the 'real' implementation of IAccountService and in tests call the mocked/stubbed implementation of IAccountService (pretty much as you are doing it now). The reason you want to "bother" with RM (or any other mocking framework) isn't often all that clear in so simple a use-case as this where you could almost just as easily create your own "public class MyMockAccountService : IAccountService" and "dummy up" your own implementation of the GetAccount() method. The power of RM (even in this use case) comes when your IAccountService interface grows over time and you would have to keep returning to all your own dummy IAccountService implementations and 'update them' to reflect the new methods on the interface or if/when it becomes important to you to verify that GetAccount() was called, called a specific number of times, called with a specific set of arguments, etc., etc. You could also add methods to "MyMockAccountService" with signatures like "bool WasCalled(string methodName, int times)" and then keep track of all that consumption "state" within your MyMockAccountService class, but this gets pretty old pretty fast (esp. if you have all kinds of different custom impls of IAccountService out there to support all kinds of different test scenarios you are interested in). Its all *that* goo that you don't want/need to concern yourself with when writing your tests that something like RM makes much less painful. Does this help make more sense of it for you --? Steve Bohlen [email protected] http://blog.unhandled-exceptions.com http://twitter.com/sbohlen On Wed, May 11, 2011 at 12:26 PM, simo <[email protected]>wrote: > Hi, > > I have been waiting to use a mocking framework for a while, > particuarly for mocking the service layer. The attraction being that > the client developers do not need to wait for completion of a service > operation before they can start testing binding in the client code. > This in effect allows parallel development of service and client based > code, allowing the client to get data via a mock service without > having to go via a real service to the db. > > After choosing Rhino Mocks as the framework of choice, I successfully > set up some client test code, whereby a mock service operation returns > test data to the client. Below is an example of such code, in which I > have basically mocked a service operation GetAccounts, which returns > IEnumerable<Account>. Account being a simple user object with simple > data types as properties (i.e. ints strings). > > My questions regarding this are as follows: > 1) Here I am pre populating the data for rhino to return (in the set > up data code region). Is is possible to get Rhino to generate this > data randomly, i.e. a random number of accounts, all with random int > and string properties already populated with random string/int values? > 2) If the above is not possible, then I am trying to understand the > point of using rhino in this instance, as surely I could just create a > list of IEnumerable<Account>, without having to additionally exposing > them via a mocked service, thus halving the code below? > > I realise that perhaps my example is not the best way to mock services > and so would be very willing to hear any different suggestions, as am > sure can use a lot more of the power in Rhino particuarly with regard > to test data, as I need to get 'buy in' from client development > department and the example given may be seen as verbose and > unecessary? > > Kind Regards > > Simon > > Code is below, which simply creates 5 test accounts and returns them > as part of the mocked service call: > > #region setup test data > List<Account> ACV = new > System.Collections.Generic.List<Account>(); > Account account = null; > int accountcount = 5; > IEnumerable<Account> accounts = null; > > for (int i = 0; i < 5; i++) > { > account = new Account() > { > AccountCode = "", > AccountID = 123, > AccountName = "" > }; > ACV.Add(account); > } > accounts = ACV; > #endregion > > #region call mock service > MockRepository mocks = new MockRepository(); > IAccountService MockAccountService = > mocks.CreateMock<IAccountService>(); > using (mocks.Record()) > { > > Expect.Call(MockAccountService.GetAccount()).Return(accounts); > } > using (mocks.Playback()) > { > IEnumerable<Account> ac = > MockAccountService.GetAccount(); > Assert.IsTrue(ac.Count() == 5); > } > #endregion > > -- > 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.
