简体   繁体   English

如何对返回XML的“存储库”进行单元测试?

[英]How should I unit test a “repository” that returns XML?

For a project I have a series of pseudo-repositories that create XML (they aren't true repositories but fill the same role so I've gone with that nomenclature) by calling some XML-returning stored procs. 对于一个项目,我有一系列创建XML的伪存储库(它们不是真正的存储库,但起着相同的作用,因此我不再使用该命名法了),方法是调用一些返回XML的存储过程。 For purposes of my project I also have "Mappers" (again they aren't true mappers...) that take the XML as input and uses Linq to translate the raw XML to DTOs. 就我的项目而言,我还有“映射器”(同样不是真正的映射器...),它们将XML作为输入并使用Linq将原始XML转换为DTO。

Since I have the mapper, it seems to me that the "repository" shouldn't be testing the value returned (since that's the job of the mapper; the repository only cares that it got some XML back, whether or not the XML data is correct). 由于我拥有映射器,因此在我看来,“存储库”不应该测试返回的值(因为这是映射器的工作;存储库仅在乎是否返回了一些XML,无论XML数据是否正确)。 However, this results in tests that are literally just making sure that the return value from the "repository" isn't null. 但是,这导致测试实际上只是确保“存储库”的返回值不为null。

Basically each repository implements an interface that has a single method called GetXml that returns an XML document. 基本上,每个存储库都实现一个接口,该接口具有一个称为GetXml方法,该方法返回XML文档。 The actual implementation does the database call, but for the test I have a very basic mock class that just returns a blank XML document. 实际的实现会执行数据库调用,但是对于测试,我有一个非常基本的模拟类,该类仅返回一个空白XML文档。 Eventually I will need to build up an actual XML file using some hard-coded values for the mock, but is it "okay" that the repository tests are essentially a single line: Assert.IsNotNull(repository.GetXml(), "Xml response was null"); 最终,我将需要使用一些硬编码的值来构建实际的XML文件,但是存储库测试本质上是单行是否“好”: Assert.IsNotNull(repository.GetXml(), "Xml response was null");

Is this something that should even be tested, or is there a better way to test this without treading on the toes of the mapper? 这是应该进行测试的东西,还是有一种更好的方法来进行测试而不踩踏制图仪的脚趾? I suppose from a design standpoint I could remove the mappers completely and just have the repository do the mapping itself (or make the mapper internal to the repository). 我想从设计的角度来看,我可以完全删除映射器,而让存储库自己进行映射(或使映射器位于存储库内部)。 I'm not doing TDD, as I actually have the code written but I'm wanting to create tests so it's easier to test, and so I can show my co-workers the benefits of tests (we don't currently use any type of automated testing). 我没有做TDD,因为我实际上已经编写了代码,但是我想创建测试,以便于测试,因此我可以向同事展示测试的好处(我们目前不使用任何类型自动测试)。

I guess what I'm really asking it this: Is it okay to write a test that only expresses a design intention to someone who may use the code, without actually caring about the value returned? 我猜想我真正要问的是:编写向可能使用该代码的人表达设计意图的测试,而实际上并不关心返回的值,这可以吗? Right now that's what these tests do: They say, in code, "I should be able to create an XmlXXXRepository class that implements an interface called IXmlRepository , takes a long called quoteID upon construction, and has a method called GetXml() that returns an XmlDocument object" and that's it. 现在,这些测试就是这样做的:他们在代码中说:“我应该能够创建一个XmlXXXRepository类,该类实现一个名为IXmlRepository的接口,在构造时采用一个长称为quoteID的类,并且具有一个名为GetXml()的方法,该方法返回一个XmlDocument对象”就是这样。

I usually write two types of tests for my XML methods. 我通常为XML方法编写两种类型的测试。 First I write unit tests for the logic involved with creating the XML result. 首先,我为创建XML结果所涉及的逻辑编写单元测试。 This usually forces you to extract your logic into another class so that the logic and the XML creation are separated. 这通常会迫使您将逻辑提取到另一个类中,以便将逻辑和XML创建分开。 Which usually means the logic ends up filling in a DTO and the XML is created off of the resulting DTO using some sort of builder class or simple XML serializer. 这通常意味着逻辑最终会填充DTO,并使用某种生成器类或简单的XML序列化程序从生成的DTO中创建XML。

Once the unit tests all work, I start creating integration tests with a known set of inputs and known XML outputs. 一旦单元测试完成所有工作,我就开始使用一组已知的输入和XML输出来创建集成测试。 Depending on the XML result, this can be either very simple text comparison or a series of XPath queries to validate the structure and the values in the XML. 根据XML结果,这可以是非常简单的文本比较,也可以是一系列XPath查询,以验证XML中的结构和值。

Oh and the XML assertions in MBUnit are terrific for this kind of testing. 哦,对于这种测试,MBUnit中的XML断言很棒。

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

相关问题 我应该如何对使用Entity Framework实现的存储库方法进行单元测试? - How should I unit test a repository method implemented with Entity Framework? 我应该如何对返回模型的控制器进行单元测试? - How should I unit test a controller that returns a model? 我应该单独测试代码没有明显的方法返回吗? - Should I unit test code with no obvious returns from methods? 如何对使用 DbContext 和 NSubstitute 的存储库进行单元测试? - How do I unit test a repository that uses DbContext with NSubstitute? 如何对返回 ToList 的操作进行单元测试 - How do I unit test an action that returns a ToList 如何对返回无效值的命令处理程序进行单元测试? - How do I unit test a Command Handler that returns a void? 如何对返回IEnumerable的方法进行单元测试 - How do I Unit Test a method that returns IEnumerable 我如何单元测试返回PartialViewResult的MVC Action? - How can I unit test an MVC Action that returns a PartialViewResult? XUnit 如何对业务对象进行单元测试,我应该嘲笑一切吗? - XUnit how to unit test business object and should I be mocking everything? 我应该如何检查工厂在单元测试中创建的 object 图 - How should I inspect the object graph created by a factory in a unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM