我是Nunit测试的新手。 我想写一个在数据库中插入数据的方法的测试方法。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();
以红色突出显示。
我不知道最近出了什么问题,因为我的项目建立成功,但当我运行测试时出现错误。
运行NUNIT时通常会有一个堆栈跟踪。此外,SqlHelper.GetConnectionString()是否有单元测试时没有正确实例化的任何外部依赖关系? – Alex 2013-02-18 07:58:15