neel
neel

Reputation: 5293

Unit test in EF data model class

hi i am generated an CRUD operations in entity frame work in mvc4. Now i Unit test that classes .. i am using the following code in controller for creation

[HttpPost]
public ActionResult Create(Member member)
{
    if (ModelState.IsValid)
    {
        db.Members.Add(member);
        db.SaveChanges(); 
        return RedirectToAction("Index");
    }

    return View(member);
}

and i am using the test code for testing this is,

[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"]);
}

i am just try to test the create class. but it shows the following error

Test failed: Create

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)

This is my connection string

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

Normally Create operation is worked with this connection string. Can anybody please help me to identify the problem. Thank you

Upvotes: 0

Views: 361

Answers (1)

Amit
Amit

Reputation: 15387

Please add the connection string in test case project and your return action is Create not the Index.

Upvotes: 1

Related Questions