您好,我在mvc4中的實體框架工作中生成了一個CRUD操作。現在我單元測試的類..我用下面的代碼在控制器創造EF數據模型類中的單元測試
[HttpPost]
public ActionResult Create(Member member)
{
if (ModelState.IsValid)
{
db.Members.Add(member);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(member);
}
,我使用測試,這是測試代碼,
[TestMethod]
public void Create()
{
MemberController me = new MemberController();
var mem = new Member();
mem.MemID = 123;
mem.MemName = "sruthy";
var result = (RedirectToRouteResult)me.Create(mem);
Assert.AreEqual("Index", result.RouteValues["action"]);
}
我只是嘗試測試創建類。但它顯示了以下錯誤
測試失敗:創建
Message: Test method SmpleTest.MemberTest.Create threw exception: System.data>ProviderIncomactibleException:An error occured while getting provider information from the database. This can be cased by Entity Framework using an incorrect connection string. Check the inner exception for details and ensure that the connection string is correct.--->System.data.ProviderIncompatibleException:The provide did not return a ProviderManifestToken string.---> System.Data.SqlClient.SqlException:A network- related or intace specific error occured while establishing a connection to SQL Server. The server was not found or was not accesable. Varify that the instance name is correct and the SQL Server is configured to allow remote connections.(proider:SQL Network Interfaces, error:26-Error Locating Server/Instance Specified)
這是我的連接字符串
<connectionStrings>
<add name="SampleDataContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Sample;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Sample.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
通常創建操作與此連接字符串的工作。任何人都可以幫助我找出問題。謝謝
供參考:這不是一個單元測試,它是一個集成測試 – MUG4N