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

  • Home
  • SEARCH
  • 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 6919149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:57:10+00:00 2026-05-27T09:57:10+00:00

Ok, I have an Nunit test class setup that runs though a method, lets

  • 0

Ok, I have an Nunit test class setup that runs though a method, lets all it CalcBalance(). Within CalcBalance() we have another method that will save things to a database. I do not want to deal with the database when testing this particular method, but when I run my tests the database method throws an exception and causes the test to fail because the databases isn’t setup/mocked. Example:

CalcBalance()
{

//Stuff I want to test

DatabaseInteraction()     //Throws exception

 return;         //This is the value I want to have tested.

}

My question is, is there any way I can set this up so that the Nunit tester will continue to test through even though the database interaction is saying it failed. I commented out DatabaseInteraction() and ran the test and it went through fine.

  • 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-05-27T09:57:11+00:00Added an answer on May 27, 2026 at 9:57 am

    The functionality inside DatabaseInteraction which is causing the exception should be abstracted out to another class which implements an interface. This interface can then be mocked for use in unit testing.

    For example:

    public class BalanceCalculator
    {
        readonly IDatabaseInteraction _databaseInteraction;
    
        public BalanceCalculator(IDatabaseInteraction databaseInteraction)
        {
            _databaseInteraction = databaseInteraction;
        }
    
        public Decimal CalcBalance()
        {
            //Stuff I want to test
    
            _databaseInteraction.Interaction();
    
            return 5.0D;         //This is the value I want to have tested.
        }
    }
    

    Then in the unit test, it can be done like this (using Moq):

    [Test]
    public void CheckValueTest()
    {
        Mock<IDatabaseInteraction> mockInteraction = Mock<IDatabaseInteraction>();
        BalanceCalculator balanceCalculator = new BalanceCalculator(mockInteraction.Object);
    
        decimal result = balanceCalculator.CalcBalance();
        Assert.AreEqual(5.0D, result);
    }
    

    The IDatabaseInteraction is mocked to do nothing, so there is no chance of an exception or any unrelated functionality taking place.

    For further information, read up on Inversion of Control, it is really useful for testability.

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

Sidebar

Related Questions

I have some assembly that references NUnit and creates a single test class with
I have an nunit Test in C#, that calls a C# wrapper of a
I am using C# 2.0 with Nunit Test. I have some object that needs
Does MSTest have standalone GUI similar to nUnit that lets me use it and
I have 1 unit test method that needs several parameters. I would like to
I'm using NUnit for my unit tests and I have my unit test class
I have a private method that should return true.I m using Nunit and MOQ
I have a Nunit test which adds message into MSMQ. In the teardown of
I have a problem: if i run this test in NUnit ,it works Board
I have an MSBuild script that uses NUnit to run tests in two assemblies.

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.