Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:34:55+00:00 2026-06-10T23:34:55+00:00

I need help testing the following code public virtual void Update(T entity) { if

  • 0

I need help testing the following code

public virtual void Update(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }

        int iretries = 0;
        bool success = false;

        do
        {
            try
            {
                this.context.SaveChanges();
                success = true;
            }
            catch (DbUpdateConcurrencyException ex)
            {
                // Get the current entity values and the values in the database
                // as instances of the entity type
                var entry = ex.Entries.Single();
                var databaseValues = entry.GetDatabaseValues();

                // Choose an initial set of resolved values. In this case we
                // make the default be the values currently in the database: StoreWins
                object resolvedValues = ResolveConcurrency(databaseValues.ToObject());

                // Update the original values with the database values and
                // the current values with whatever the user choose.
                entry.OriginalValues.SetValues(databaseValues);
                entry.CurrentValues.SetValues(resolvedValues);

                // give up after n retries
                if (++iretries == NUMBER_OF_CONC_RETRIES)
                    throw;
            }
            catch (Exception)
            {
                //rethrow 
                throw;
            }
        } while (!success);
    }

I want to unit test the DbUpdateConcurrencyException branch.

So, one simple test scenario would be:

  • Creating a new DbUpdateConcurrencyException
  • Mock the SaveChanges to throw the above exception
  • Verify that SaveChanges was called a number of NUMBER_OF_CONC_RETRIES
  • Assert that the Update method re-throws the exception

In the current state, the above test scenario cannot be tested, I cannot mock the exception to contain a IEnumerable<DbEntityEntry> with a single DbEntityEntry; I cannot mock the GetDatabaseValues(), etc.

A simple solution would be to insert a new layer of abstraction; let’s say using an interface to abstract the entire code that currently sits in the catch block, and to provide a mock that does nothing.

But then I would end up in the situation when I would want to test the implementation of that interface, and would end up having the same questions as above. How can I mock the DbUpdateConcurrencyException, the GetDatabaseValues, etc.

I am using moq for mocking.

Thank you for your input

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:34:56+00:00Added an answer on June 10, 2026 at 11:34 pm

    If you cannot mock something you must hide it behind something else you can mock or override in test. Your test actually doesn’t need to use all that stuff for loading values and setting them in the entry – that is all dependent on EF and you will not test it when mocking the context because that would mean to re-implement EF’s logic behind SaveChanges. All you need to do is:

    catch (DbUpdateConcurrencyException ex) {
        RefreshValues(ex);
    
        // give up after n retries
        if (++iretries == NUMBER_OF_CONC_RETRIES)
            throw;
    }
    

    Where RefreshValues can be protected virtual method and you can override it in your test either by providing test version of the class (you can even achieve this with Moq) or by inheriting the test from this class and overriding the method directly in test class.

    To setup Moq you need interface for your context exposing SaveChanges method:

    var contextMock = new Mock<IContext>();
    contextMock.Setup(m => m.SaveChanges())
               .Callback(m => throw new DbUpdateConcurrencyException());
    

    If you need to test that it works for few throws as well you need to keep counter in the test and use it in the callback to decide if throw or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to the world of WhiteBox Testing and need help designing a
After much googling and console testing I need some help with arrays in rails.
Need help on these 2 questions: If i am tracking a virtual pageview for
I need help with re module. I have pattern: pattern = re.compile('''first_condition\((.*)\) extra_condition\((.*)\) testing\((.*)\)
I'm testing the following code to populate a dictionary recursively. However the type inference
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
Need help with a query that I wrote: I have three tables Company id
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
Need help writing a script downloads data from google insight using c# this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.