How do I use Moq and DbFunctions in unit tests to prevent a NotSupportedException?

前端 未结 7 1671
借酒劲吻你
借酒劲吻你 2020-12-05 23:05

I\'m currently attempting to run some unit tests on a query that is running through the Entity Framework. The query itself runs without any issues on the live version, but t

相关标签:
7条回答
  • 2020-12-05 23:58

    Use of Mocks ended sometime ago. Do not Mock, just connect to real DB. Regenerate/Seed DB on start of test. If you still want to go ahead with mocks then create your own method as given below. IT changes behaviour runtime. When using real DB it uses DB functions, else this method. Replace DBfunctions method in code with this method

    public static class CanTestDbFunctions
    {
        [System.Data.Entity.DbFunction("Edm", "TruncateTime")]
        public static DateTime? TruncateTime(DateTime? dateValue)
        {
            ...
        }
    }
    

    This is the real function that is called. And remember, time cannot be removed from DateTime object, live with midnight or create a string equivalent.

    0 讨论(0)
提交回复
热议问题