开发者

How to use Moles to stub entity framework stored procedure call?

开发者 https://www.devze.com 2023-01-11 20:16 出处:网络
I\'m tryin开发者_高级运维g to stub a call to db. The basic idea is for a line of code like this:

I'm tryin开发者_高级运维g to stub a call to db. The basic idea is for a line of code like this:

Person person = (from p in this.Entities.FindPerson("Smith") select p).FirstOrDefault();

to return an object the way I want it without going of to db. FindPerson(string) represents a stored proc (just in case).

I tried to overwrite FindPerson but I need to return ObjectResult. It's a sealed class with no public constructors. All my attempts to create it ended with a call to db.


I was looking for an answer for the same question. Following forum thread cleared it out for me: msdn forum

Basically they are saying to not make calls to EF directly, but to make your code testable by abstracting your data layer from your business layer.

You can do this by Repository pattern or something similar and then use Dependency Injection to inject a mock.

Quote from Peli (Microsoft employee, involved in the "Pex" program):

Moles should be a last resort solution. The preferred way is to use a testable design, i.e. abstraction between the data layer and business layer etc...

HTH

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号