我是 Nunit 测试的新手。我正在尝试为在数据库中插入数据的方法编写测试方法。
我的代码是这样的:
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()
{
}
}
实际的 TagStore 代码就像
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;
}
}
当我运行测试时出现错误:TagStoreTest.InsertTagTest:
System.NullReferenceException : Object reference not set to an instance of an object.
代码行TagStore tagSt = new TagStore();
以红色突出显示。
我不知道出了什么问题,因为我的项目构建成功,但是在运行测试时出现错误。