0

I'm using microsoft.visualstudio.testtools 10.0 and EntityFramework 6.0,

  1. I have the method "validateAlreadyEnroll" which executes a linq query.
  2. I have a unit test "testAlreadyEnroll" which calls "validateAlreadyEnroll"
  3. testAlreadyEnroll fails or succeeds if I comment the linq query, otherwise it crashes.
  4. 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");
    }

Part of the app.config file

This is the error I get

  • Please post the specific error message and stack trace. That is what would give us clues as to what the issue may be. – Kevin Nov 07 '16 at 18:11
  • I added the error as an image since I couldn't copy and paste (is a lot), thanks Kevin! I completely forgot to add it. – Lourdes Valladares Nov 07 '16 at 20:19
  • To which database is your context supposed to connect? I only see an ExcelConnection in the connection strings. – Gert Arnold Nov 07 '16 at 21:17
  • Hello Gert! I deleted the connection strings on purpose to upload the screenshot :D It works fine now, I needed to ensure a DLL was being copied. – Lourdes Valladares Nov 10 '16 at 23:55

2 Answers2

0

Looking at the error code, I think your project or database configuration is wrong. Maybe add reference to assemblies.

Also, if you want to do test driven development properly, you should have much smaller test cases. For instance one that just checks db.Enrollments.Count() > 0 for your data source. That will quickly pinpoint problems.

// Old, incorrect suggestion: But I think that the Linq statement does not know '&&' as logical operand. Use 'and' instead.

Pieter21
  • 1,765
  • 1
  • 10
  • 22
0

Finally!

My DbContext constructor needed: var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance; but also the "name" was not being referenced correctly, also I compared to an older version and this.Configuration.LazyLoadingEnabled = false; was missing, to explain better I found this post: Buildserver can not find Entity Framework Sql Provider

Community
  • 1
  • 1