簡體   English   中英

如何在單元測試中使連接字符串恆定?

[英]How to make a connection string a constant in a unit test?

由於我需要為每個客戶使用不同的數據庫,因此我有一個動態連接字符串生成器。

當我對此進行測試時,我想模擬動態連接字符串構建器以返回特定的客戶字符串對象。 我遇到的問題是C#將連接字符串作為常量不斷出錯。

 private const string connectionString = 
 @"metadata=res://Data/CustomerModel.csdl|res://Data/CustomerModel.ssdl|
 res://Data/CustomerModel.msl;provider=System.Data.SqlClient;
 provider connection string=/""Data Source=MyDB;Initial Catalog=MyCustomer;
 User ID=SomeUser;Password=SomePassword"

當前錯誤是:

 An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but    
 was not handled in user code

 Additional information: Keyword not supported: 'initial catalog'.

我知道這是C#存儲連接字符串的方式,我不知道如何解決它。 我不想使用我擁有的“連接字符串”構建器服務,因為那樣,我的測試將不僅依賴於此“單元”。 我想偽造連接字符串生成器以返回該連接字符串。

有什么建議么?

一種選擇是使用EntityConnectionStringBuilder並手動構建它。

http://msdn.microsoft.com/en-us/library/vstudio/bb738533(v=vs.100).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM