This is a migrated thread and some comments may be shown as answers.

How to use Throws() here...

4 Answers 23 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
James Legan
Top achievements
Rank 1
James Legan asked on 18 Sep 2012, 05:17 PM
I have a method that uses an OpenAccess model. I want to mock out an OptimisticVerificationException in the first (and only first) call to SaveChanges(). How can this be achieved?

public void ThrowTest(Guid id, string stringToUpdate)
       {
           var user = _model.Users.SingleOrDefault(p => p.Guid == id);
           if (user != null)
           {
               user.StringToUpdate = stringToUpdate;
               try
               {
                   _model.SaveChanges();
               }
               catch (OptimisticVerificationException optimisticVerificationException)
               {
                   _model.Refresh(RefreshMode.OverwriteChangesFromStore, user);
                   user.StringToUpdate = stringToUpdate;
                   _model.SaveChanges();
               }
 
           }
       }

4 Answers, 1 is accepted

Sort by
0
James Legan
Top achievements
Rank 1
answered on 18 Sep 2012, 08:31 PM
Here is the solution:

// Handles the first call
Mock.Arrange(() => _mockModel.SaveChanges()).Throws<OptimisticVerificationException>(string.Empty).InSequence();
 
// Handles the second call.
Mock.Arrange(() => _mockModel.SaveChanges()).InSequence();
0
Ricky
Telerik team
answered on 21 Sep 2012, 03:44 PM
Hi James,
Thanks again for contacting us. It's great that you got the solution. However, you can also do specific Throws for different arguments.

Kind Regards
Mehfuz
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
James Legan
Top achievements
Rank 1
answered on 21 Sep 2012, 05:22 PM
Mehfuz,

Given my example, could you please provide an example of how the calls could be changed? 

Thanks,

Jim
0
Ricky
Telerik team
answered on 26 Sep 2012, 04:40 PM
Hi James,

It’s great that you asked the question. However, you are right. I haven't noticed that SaveChanges is a void method. In that regard InSequence is the answer.


Kind Regards
Mehfuz
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Tags
General Discussions
Asked by
James Legan
Top achievements
Rank 1
Answers by
James Legan
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or