I'm using microsoft.visualstudio.testtools 10.0 and EntityFramework 6.0,
- I have the method "validateAlreadyEnroll" which executes a linq query.
- I have a unit test "testAlreadyEnroll" which calls "validateAlreadyEnroll"
- testAlreadyEnroll fails or succeeds if I comment the linq query, otherwise it crashes.
- There is the app.config which sets the data driven test
Is this an issue with versions? Please help.
Main method:
public static bool validateAlreadyEnroll(int sectionid, int personid, ref string extramessage, ref validationResult vr)
{
int n = (from e in db.Enrollments where e.SectionID == sectionid && e.EnrolledPersonID == personid select e).ToList().Count();
if(n == 0){
return false;
}
var statusid2 = db.Database.SqlQuery<resultStatusID>("select top 1 EnrollmentStatusID from Enrollment where SectionID = " + sectionid + " and EnrolledPersonID = " + personid + " order by EnrollmentID desc").ToList();
if (statusid2[0].EnrollmentStatusID == CANCELREQUEST) {
return false;
}
return true;
}
Unit Test:
[TestMethod]
[DataSource("DataSource3")]
[DeploymentItem("Regi.Service.Tests\\tests.xlsx")]
public void testAlreadyEnroll()
{
int n;
int sectionid;
if (Int32.TryParse(TestContext.DataRow["sectionid"].ToString(), out n))
{
sectionid = Int32.Parse(TestContext.DataRow["sectionid"].ToString());
}
else
{
sectionid = 0;
}
int personid;
if (Int32.TryParse(TestContext.DataRow["personid"].ToString(), out n))
{
personid = Int32.Parse(TestContext.DataRow["personid"].ToString());
}
else
{
personid = 0;
}
bool answer = Helper.validateAlreadyEnroll(sectionid, personid, ref msg, ref vr);
bool result = Convert.ToBoolean(TestContext.DataRow["result"].ToString());
Assert.AreEqual(answer, result, "Failure");
}