How are people unit testing with Entity Framework 6, should you bother?

前端 未结 11 1992

I am just starting out with Unit testings and TDD in general. I have dabbled before but now I am determined to add it to my workflow and write better software.

I ask

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 18:02

    It is important to test what you are expecting entity framework to do (i.e. validate your expectations). One way to do this that I have used successfully, is using moq as shown in this example (to long to copy into this answer):

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking

    However be careful... A SQL context is not guaranteed to return things in a specific order unless you have an appropriate "OrderBy" in your linq query, so its possible to write things that pass when you test using an in-memory list (linq-to-entities) but fail in your uat / live environment when (linq-to-sql) gets used.

提交回复
热议问题