我有一個控制器正在我的站點工作,但在單元測試期間失敗。這很簡單,並依賴於Linq to Sql返回一組JSON對象。使用Linq to Sql進行MVC單元測試
測試失敗,因爲DataContext在從MVC項目外部調用時無法找到連接字符串。
我曾在汽車一看生成的代碼:
public DC():
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
Web項目可以在web.config中的「myConnectionString」項,但測試項目無法找到它。我得到的錯誤信息是:
Test method MyMVCApp.Controllers.HomeControllerTest.IndexShouldReturnIssues threw exception: System.NullReferenceException: Object reference not set to an instance of an object..
我不想通過其他連接字符串中從我的單元測試,因爲我想測試在web.config中的連接字符串的工作原理。
感謝, 約翰
看看這個答案,看看如何有效和成功地測試LINQ to SQL:http://stackoverflow.com/questions/4128640/how-to-remove-unit-of-work-functionality-from-repositories-使用-ioc/4132186#4132186 – Steven 2010-11-09 20:29:06