Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

.net - The repository itself is not usually tested?

I'm sorry but I'm new to repositories patterns, unit tests and orm tools.

I've been researching on unit tests and the repository pattern, and arrived at some conclusions, I wonder if I'm right.

The repository pattern facilitates unit testing to be replaced in controller that make use of it for example, right? Because create a stub/fake of context(in EF) or session(in NH) is harder, right? The repository itself is not tested? Why?

Using EntityFramework or NHibernate with repository pattern, if i want to test my repositories I need to do integration tests? Because if I use a fake implementation of my context/session I'm not doing real tests? Because the context/session itself is the repository (I mean they implement the real logic of Add, Remove, Edit, GetById, GetAll, ..)?

The repository pattern with EF or NH is like a wrapper? (Not only a wrapper, I know this is a import concept of the domain.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Repository interface belongs to domain layer. But implementation belongs to infrastructure or data access layer. This implementation is usually not tested with unit test. It uses ORM API heavily, so it is extremely hard and wasteful to test repository in isolation. This problem is not specific to repositories: Don't mock types you don't own.

Repository should be tested with integration tests, using real ORM. In memory database is a very popular approach to this problem.

... Because if I use a fake implementation of my context/session I'm not doing real tests?

Even if you manage to make it (which I really doubt in NHibernate's case) you will be wasting your time. Session/Context interface is out of your control and your test would just reiterate your guess about how the real thing will work.

Becausethe context/session itself is the repository?

No. Context/Session is implementation of UnitOfWork pattern. It is not part of your domain. This is infrastructure.

The repository pattern with EF or NH is like a wrapper only?

Repository is important domain concept, it is not just a 'wrapper'. Just like your application is not a 'wrapper' over the database. I think that DDD repository interface definition should be based on Ubiquitous Language as much as possible and should not include any ORM related words or types.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...