我目前正在研究一個繼承的代碼庫。關鍵的缺失部分之一是單元測試。我試圖在NUnit中設置一些單元測試,似乎遇到了障礙。單元測試帶有由SubSonic生成的DAL的應用程序
我創建了一個單獨的單元測試項目,並添加了對SubSonic,NUnit和應用程序創建的各種DLL的必要參考,並設置了一個虛擬單元測試以確保所有設置都正確。當我試圖引用由SubSonic生成的一些對象時,問題就開始了。我創造了這個測試列表中的用戶:
[Test]
public void CanListUsers()
{
UserCollection users = UserController.List(UserController
.Query()
.Where(User.Columns.IsDeleted, false));
Assert.IsNotNull(users);
}
,並得到此異常:
Can't find the SubSonicService in your application's config
我固定,通過拉出這是亞音速到一個app.config相關的Web.config的部分在單元測試項目中。現在,當我重新運行單元測試,我得到:
UnitTests.TestClass.CanListUsers: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> System.Configuration.ConfigurationErrorsException : Could not load type 'Utility.SqlSubsonicProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
這個例外有我困惑,因爲SqlSubsonicProvider是本實用命名空間中的類,並且可以在對象瀏覽器,從而可以看出爲什麼它正在系統尋找。網?
編輯:好的,我重新安排瞭解決方案中的命名空間,以便它們更有意義。我認爲這解決了上述錯誤。不幸的是,我現在收到此錯誤:
ChannelMechanics.UnitTests.TestClass.CanListVendors:
System.Reflection.TargetInvocationException : Exception has been thrown by the target
of an invocation.
----> System.NullReferenceException : Object reference not set to an instance of
an object.
什麼是更奇怪的是,當我在調試菜單使用Visual Studio的「附加到進程」命令,附加到NUnit的GUI的單元測試通過。我的理論是,空對象很容易從調試器中發現。
如果有幫助,我的App.config看起來是這樣的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="SubSonicService"
type="SubSonic.SubSonicSection, SubSonic"
requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="DatabaseConnection"
connectionString="*removed*"/>
</connectionStrings>
<SubSonicService defaultProvider="TestProvider">
<providers>
<clear />
<add name="TestProvider"
type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="DatabaseConnection"
generatedNamespace="Test"/>
</providers>
</SubSonicService>
</configuration>
異常的詳細信息是:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type,
Boolean publicOnly, Boolean noCheck, Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis,
Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance[T]()
at SubSonic.ActiveController`2.Query()
at UnitTests.TestClass.CanListVendors() in UnitTests\TestClass.cs:line 59
--NullReferenceException
at DataAccess.Vendor.GetTableSchema() in DataAccess\Generated\Models\Vendor.cs:line 376
at DataAccess.Vendor.SetSQLProps() in DataAccess\Generated\Models\Vendor.cs:line 42
at DataAccess.Vendor..ctor() in DataAccess\Generated\Models\Vendor.cs:line 35
,我運行的測試基本相同,上面列出的一個除了應該獲得列出的供應商而不是用戶。
[Test]
public void CanListVendors()
{
VendorCollection vendors = VendorController.List(
VendorController
.Query()
.Where(Vendor.Columns.IsDeleted, false));
Assert.IsNotNull(vendors);
}
是的,我認爲這個項目中的命名空間肯定有些奇怪的東西。我已經使用Microsoft Northwind創建了一個測試項目,並且能夠按預期連接到它。我想這是處理別人代碼的樂趣之一 - 在意想不到的地方發生意外錯誤! – Malice 2011-05-16 07:42:12