locked
How to Unit Test the Domain Classes that Use Data Annotations RRS feed

  • Question

  • My domain class has some data annotations applied to its properties, such as Maxlength, Range, etc.

    If I instantiate an object of this type in a console application, the data annotations do not work. So, I need to create a DbContext and then should try to insert the newly created object (which has atrocious values). Now the application throws the error and that is fine.

    How can I run unit tests on domain classes to see if the data annotations are working fine?
    • Moved by Caillen Thursday, May 29, 2014 10:08 AM
    Tuesday, May 27, 2014 4:34 PM

Answers

  • The problem with unit testing project is that I have to instantiate a DbContext data access layer to check the data annotations. This is what I did in the dummy console application. However, I wonder if there are any workarounds for this kind of testing in MS UT.

    [Test Method]

    public void TestDal()

    {

         var dal = new DAL();

         var someobj = new SomeObj()

          someobj = "make code fail";

         dal.Save(someobj);

         //Do a try/catch around the save to catch exception or

        // something to verify that validation error was rasiesd

    }

    That's exactly what you have to do. What it means is t whatever you did in the console application to make the code fail there is what you have to do in the UT or as I call it a FT or IT.

    You are working the code with a test project UI or whatever you want to call it to make the real code fail. If the DBcontext is in a DAL method, then you do it for real, which is not a UT. It's Functional or Intergartion test.

    Tuesday, May 27, 2014 6:42 PM

All replies

  • You use a UT harness like MS UT or Resharper using Nunit, you make a classlib project call it FunctionalTest or UnitTest if you like. The classlib project should be a project that's part of the solution that you are trying to test.

    You set refernce to the project you are trying to test in FunctionalTest or UnitTest, you make a test method that is going to make calls to the project and code to be tested passing whatever it needs to make the tested code work or fail.

    MS UT or Reharper hosts the classlib project FunctionalTest or UnitTest which is run through VS. You can run a method FunctionalTest or UnitTest and test the code that is to be tested in the project to be tested

    To me in this situation,  you are not doing a UT. You are doing Functional or Intergratuion testing.

    Tuesday, May 27, 2014 5:21 PM
  • The problem with unit testing project is that I have to instantiate a DbContext data access layer to check the data annotations. This is what I did in the dummy console application. However, I wonder if there are any workarounds for this kind of testing in MS UT.
    Tuesday, May 27, 2014 5:29 PM
  • The problem with unit testing project is that I have to instantiate a DbContext data access layer to check the data annotations. This is what I did in the dummy console application. However, I wonder if there are any workarounds for this kind of testing in MS UT.

    [Test Method]

    public void TestDal()

    {

         var dal = new DAL();

         var someobj = new SomeObj()

          someobj = "make code fail";

         dal.Save(someobj);

         //Do a try/catch around the save to catch exception or

        // something to verify that validation error was rasiesd

    }

    That's exactly what you have to do. What it means is t whatever you did in the console application to make the code fail there is what you have to do in the UT or as I call it a FT or IT.

    You are working the code with a test project UI or whatever you want to call it to make the real code fail. If the DBcontext is in a DAL method, then you do it for real, which is not a UT. It's Functional or Intergartion test.

    Tuesday, May 27, 2014 6:42 PM