Patrick,

Thank you for the response.

1) Up until this point, the class that has handled the very transactional 
work of performing my CRUD operations has not needed to be stateful and is 
static. If that needs to change in order to achieve what I am after, so be 
it.

2) VerifyModel, similar to the dbContext of an EF based project is 
Telerik's database context object which is created with generating their 
.rlinq designer file (liken it to an EF .edmx file).

3) .Add and .SaveChanges are transactional database workers of OA (along 
with delete, flushchanges, etc...). I was already considering dependency 
injecting the ObjectScope in an overloaded constructor so that I could pass 
in a mocked ObjectScope which would own the transaction over its lifecycle. 
I am not sure what the behavior would be of mocking the VerifyModel but it 
is an interesting thought. Have you ever done something similar with the 
dbcontext of an EF project?

4) Sorry, that like was part of a ConcurrentDictionary caching scheme and 
should not have made it into my sample.

Thanks,

Jim


On Thursday, September 13, 2012 7:53:06 AM UTC-4, Patrick Steele wrote:
>
> There's not much to go on here, but a few things I noticed: 
>
> 1. The methods you showed are static.  At some point, you may want to 
> mock out these methods when testing other parts of your code.  Static 
> methods are not mockable with Rhino.Mocks.  You should make the 
> virtual instance methods. 
> 2. What is the "VerifyModel" you create? 
> 3. If model.Add() and model.SaveChanges() are virtual, you can mock 
> them, but you'd have to change the way the 'model' is created (inject 
> it or provide a factory to create it). 
> 4. "UserContainer.Instance.Refresh()" --> looks like another static 
> method.  Those can't be mocked/stubbed with Rhino.Mocks 
>
> --- 
> Patrick Steele 
> http://weblogs.asp.net/psteele 
>
>
> On Wed, Sep 12, 2012 at 9:25 PM, jdl <[email protected] <javascript:>> wrote: 
> > All, 
> > 
> > I have been going back and form with the team at Telerik that put out 
> > JustMock as I am evaluating several mocking frameworks right now. I 
> spend 
> > most of my day working in business layer logic in IIS hosted WCF 
> services. I 
> > am attempting to break my SQL server dependecy in my unit tests by 
> mocking 
> > out the db context. A Rhino Mocks users essentially achieved what I am 
> > looking for here in 2009 using an old implementation of OpenAccess here: 
> > 
> http://www.reversealchemy.net/blog/2009/03/17/exploring-telerik-openaccess-unit-testing-your-business-logic-using-rhino-mocks/.
>  
>
> > The issue is several fold but the root of it is that OA has 
> fundamentally 
> > changed its implementation since then and I cannot seem to get it to 
> jive 
> > quite right with either JustMock or not Rhino Mocks. I have emailed the 
> > original author of the article to see if he has an updated example and 
> will 
> > post his response here should he respond. Ultimately what it boils down 
> to 
> > is that I have methods that perform business logic that may or may not 
> > add/update entities (hacked example code below). What I am trying to 
> achieve 
> > (and it simply may not be possible given my end goal and I apologize as 
> I am 
> > relatively new to mocking) is to break the dependency on a DB on the 
> other 
> > end while still leveraging the code as if it were (i.e Add an entity to 
> the 
> > context but not actually commit it OR when an add occurs on an entity 
> whose 
> > ID is a uniqueidentifier (guid), return a new guid to simulate success 
> which 
> > allows the update method to use that entity's guid to perform the update 
> > operation). 
> > 
> > Please let me know if you need a more extensive code example. I am 
> hopeful 
> > that Erik's post and the sample methods below will highlight what I am 
> > trying to do, something that I have not yet successfully conveyed to 
> > Telerik. 
> > 
> > public static Guid AddUser(string name, int legacyId = -1) 
> >         { 
> >             try 
> >             { 
> >                 using (var model = new VerifyModel()) 
> >                 { 
> >                     User userEntity = GetUserEntity(name); 
> > 
> >                     if (userEntity != null) 
> >                     { 
> >                         return userEntity.Guid; 
> >                     } 
> > 
> >                     var newUser = new User 
> >                     { 
> >                         Name = name, 
> >                         LegacyId = legacyId 
> >                     }; 
> > 
> >                     model.Add(newUser); 
> >                     model.SaveChanges(); 
> >                     UserContainer.Instance.Refresh(); 
> >                     return newUser.Guid; 
> >                 } 
> >             } 
> >             catch (Exception exception) 
> >             { 
> >                 ErrorLogWriter.Write(MethodBase.GetCurrentMethod().Name, 
> > exception); 
> >             } 
> > 
> >             return Guid.Empty; 
> >         } 
> > 
> > Update method handles optimistic verification exceptions but at the end 
> of 
> > the day simply performs a sql update. 
> > 
> > public static bool UpdateUserDetails(Guid userGuid, 
> > IEnumerable<ElementAttributeValues> elementAttributeValues) 
> >         { 
> >             try 
> >             { 
> >                 var historyEvents = new List<string>(); 
> > 
> >                 using (var model = new VerifyModel()) 
> >                 { 
> >                     var user = model.Users.SingleOrDefault(p => p.Guid 
> == 
> > userGuid); 
> > 
> >                     if (user == null) 
> >                     { 
> >                         return false; 
> >                     } 
> > 
> >                     var enumeratedElementAttributeValues = 
> > elementAttributeValues asList<ElementAttributeValues> ?? 
> > elementAttributeValues.ToList(); 
> > 
> >                     try 
> >                     { 
> >                         
> UpdateUserEntity(enumeratedElementAttributeValues, 
> > ref user, refhistoryEvents); 
> > 
> >                         model.SaveChanges(); 
> >                     } 
> >                     catch (OptimisticVerificationException 
> > optimisticVerificationException) 
> >                     { 
> >                         ErrorLogWriter.Write(string.Format("An 
> optimistic 
> > verification exception occured in {0} and the commit will be retried. 
> The 
> > error message is: {1}.", 
> > 
> > MethodBase.GetCurrentMethod().Name, 
> > 
> > optimisticVerificationException.Message)); 
> > 
> >                         
> model.Refresh(RefreshMode.OverwriteChangesFromStore, 
> > user); 
> > 
> >                         // retry update 
> >                         
> UpdateUserEntity(enumeratedElementAttributeValues, 
> > ref user, refhistoryEvents); 
> > 
> >                         model.SaveChanges(); 
> >                     } 
> > 
> >                     foreach (string historyEvent in historyEvents) 
> >                     { 
> >                         HistoryWriter.Write(userGuid, historyEvent); 
> >                     } 
> > 
> >                     return true; 
> >                 } 
> >             } 
> >             catch (Exception exception) 
> >             { 
> >                 ErrorLogWriter.Write(MethodBase.GetCurrentMethod().Name, 
> > exception); 
> >             } 
> > 
> >             return false; 
> >         } 
> > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Rhino.Mocks" group. 
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msg/rhinomocks/-/QqeKVDy-DTkJ. 
> > To post to this group, send email to 
> > [email protected]<javascript:>. 
>
> > To unsubscribe from this group, send email to 
> > [email protected] <javascript:>. 
> > 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 view this discussion on the web visit 
https://groups.google.com/d/msg/rhinomocks/-/VE8_Mxy-fJMJ.
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