简体   繁体   English

如何以适当的方式使用Ninject测试.net项目

[英]How to test .net project with Ninject in proper way

We have a project with separated bussiness layer. 我们有一个分离商务层的项目。 It's like lots of services (classes) in separated project in the solution. 这就像解决方案中分离项目中的许多服务(类)一样。 Also we use ninject to manage dependancies. 我们还使用ninject来管理依赖性。 All classes in bussiness layes project are internal, and it communicates with «another world» through interfaces. 商业项目中的所有课程都是内部的,它通过界面与“另一个世界”进行交流。 If to create new project that would contain test then it wont see internal classes (but yeah we can do a hack and declare Internal to Public in AsseblyInfo). 如果要创建包含测试的新项目,那么它就不会看到内部类(但是我​​们可以做一个hack并在AsseblyInfo中声明Internal to Public)。

What i really need to know is what's neccessary to test: 我真正需要知道的是测试的必要条件:

  1. We can create test envirement of everything, and test only through produced interfaces (there is no «clear» DAL, we are using linq2sql, but its possible to be mocked) This way looks goods, because we know nothing about internal BisLayer structure and test only «contract» functionality. 我们可以创建所有东西的测试环境,并且只通过生成的接口进行测试(没有«clear»DAL,我们使用的是linq2sql,但它可能被嘲笑)这种方式看起来像商品,因为我们对内部BisLayer结构和测试一无所知只有«合同»功能。 But the bad side is that the system has lots of options, settings and bindings and it seems impossible or pretty hard to check all possible variants of it 但不好的一面是系统有很多选项,设置和绑定,看起来不可能或很难检查它的所有可能的变种

  2. We can place tests in the same project or set attribute to make internal being seen as public, so we'd be able to test internal classes. 我们可以在同一个项目中设置测试或设置属性以使内部被视为公共,因此我们可以测试内部类。 Its good because we can test almost everything, but its hard to control bindings, cos it'd be nice Ninject to do it, and we would only override bindings we need in concret test. 它很好,因为我们可以测试几乎所有东西,但它很难控制绑定,因为它很好Ninject这样做,我们只会覆盖我们在混凝土测试中需要的绑定。 Also its not clear how to test classes implementing the same interface (and doing similar things). 还不清楚如何测试实现相同接口的类(以及做类似的事情)。 Like we have few implementations of Cache but each impl-tion keeps data in different places (mssql, key-value db, asp cache, etc), so tests for each implementation actually would be the same 就像我们几乎没有Cache的实现,但每个impl-tion将数据保存在不同的地方(mssql,键值db,asp缓存等),所以每个实现的测试实际上是相同的

As you say you need to have access to the classes in order to test. 正如您所说,您需要访问类才能进行测试。 So, make only the internals that are exposed to the outside trough interfaces accessible from the outside. 因此,只能从外部访问暴露于外部槽接口的内部构件。

Write your tests only against the behaviour that is exposed to the outside, "another world" as you call it. 仅针对暴露于外部的行为,即您称之为“另一个世界”的行为来编写测试。 Write the more generic test cases first and the go into details as needed. 首先编写更通用的测试用例,然后根据需要详细说明。 As this will be an ongoing process together with the development/change of the actual functionality you'll the be able to decide how many fine grained scenarios you actually need. 由于这将是一个持续的过程以及实际功能的开发/更改,您将能够决定实际需要多少细粒度场景。

Also take a look at Ninject Mocking Kernel extension https://github.com/ninject/ninject.mockingkernel 另请参阅Ninject Mocking内核扩展https://github.com/ninject/ninject.mockingkernel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM