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 9323737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:25:09+00:00 2026-06-19T04:25:09+00:00

I am new to Nunit testing. I am trying to write test method for

  • 0

I am new to Nunit testing.
I am trying to write test method for a method which inserts data in Database.

My code is like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;
using MBT.Entities;


[TestFixture]
public class TagStoreTest
{
    private Tag testTag;

    public TagStoreTest()
    {
      this.testTag = new Tag();
    }

[Test]
public void InsertTagTest()
{
    TagStore tagSt = new TagStore();
    bool isInserted = tagSt.InsertTag(this.testTag);

    Assert.IsTrue(isInserted);
}


[SetUp]
public void Setup()
{
    this.testTag.TagName = "testtagthroughNunit";
}

     [TearDown]
     public void TearDown()
     {

     }

 }

And actual TagStore code is like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MBT.Datastore;
using System.Data;

using MBT.Entities;


  public class TagStore
 {
   private string _connectionString = string.Empty;

   public TagStore()
   {
    this._connectionString = SqlHelper.GetConnectionString();
    }


    public bool InsertTag(Tag tag)
   {
     bool isInserted = false;

     using (DatabaseHelper helper = Utility.DbHelper)
     {
        int tagsAdded = helper
            .AddInputParameter("@Tag", tag.TagName)
            .AddInputParameter("@ParentTagId", tag.ParentTagId)
            .ExecuteNonQuery("Proc_InsertTags", CommandType.StoredProcedure);

        if (tagsAdded > 0)
        {
            isInserted = true;
        }
    }
    return isInserted;
 }
}

When i run test i get error: TagStoreTest.InsertTagTest:
System.NullReferenceException : Object reference not set to an instance of an object.

And code line TagStore tagSt = new TagStore(); is highlighted in red.

I dont know whats going wrong because my project builds successfully but i get error when i run test.

  • 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-19T04:25:10+00:00Added an answer on June 19, 2026 at 4:25 am

    I see your TagStore constructor is setting connectionString member. Try to debug what is returned by

    SqlHelper.GetConnectionString();
    

    Your SqlHelper could be playing tricks.

    I also suggest try using some sort of mocking technique to isolate tests. For e.g. you can mock the SqlHelper object which always returns a desired connectionstring. There are plenty of framework out there for e.g. Moq.

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

Sidebar

Related Questions

I'm new to unit testing and nUnit (2.48). I'd like to write a test
I have problem with testing classes, which using UpdateModel() method. I get System.NullReferenceException. I
I'm trying to setup a test using NUnit to perform some integration testing of
I truly love NUnit's new(er) ability to test for expected exception testing, ie: var
I am unit-testing private methods in C# using NUnit. For example, my method (if
I am trying to write a unit test to cover code found in our
I am new to the Unit Testing And using Nunit i dont know how
I am using ASP.NET MVC 3 and NUnit . I am trying to write
I'd like to get the currently executing NUnit test in a helper method I'm
I'm new to unit testing using nunit (and Java development in general). When creating

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.