我是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