简体   繁体   English

当这些状态是内部属性时,如何使用单元测试创​​建测试状态?

[英]How do I create test states with unit testing when those states are internal properties?

I have a separate class library that I'm using for all of my unit testing (using nunit). 我有一个单独的类库,我正在使用我的所有单元测试(使用nunit)。 In the test project, I have a helper class that creates test objects for me. 在测试项目中,我有一个帮助程序类,为我创建测试对象。 When setting up these objects, I need to set some values that I have marked as internal. 设置这些对象时,我需要设置一些我标记为内部的值。 The reason I do this is because these objects are actually part of a work flow so I would only otherwise be able to achieve these property states by actually going through the workflow which kind of kills the idea of unit testing. 我这样做的原因是因为这些对象实际上是工作流程的一部分,所以我只能通过实际完成工作流程来实现这些属性状态,这种工作流程会破坏单元测试的想法。

For instance, the method I'm testing first. 例如,我首先测试的方法。 It checks to see if a certain approval was made and if it does, it moves it to the next step in the chain (or rather, it checks to see if the next approval was received, and so on down the chain). 它会检查是否有某个批准,如果有,它会将其移动到链中的下一步(或者更确切地说,它检查是否收到了下一个批准,依此类推)。 If it doesn't not, it reassigns it to the person from whom the approval is needed. 如果不是,则将其重新分配给需要批准的人。

So what I do is create a test ticket that is in a certain stage of this process. 所以我要做的是创建一个处于此过程某个阶段的测试票据。 For instance, the first test runs the method where the first approver has not approved, and checks to ensure that it gets reassigned to that person. 例如,第一个测试运行第一个批准者未批准的方法,并检查以确保将其重新分配给该人。 The next test will run it where the approver HAS approved and ensures that it moves on down the tree. 下一个测试将在批准者HAS批准的地方运行,并确保它在树下移动。

The problem that I obvious face is that the test classes are in a different namespace, so I can't hit the internal properties. 我明显面临的问题是测试类位于不同的命名空间中,因此我无法点击内部属性。 I'm not sure if there's a work around for this or if maybe I'm missing some other fundamental that is backing me into this corner (design smell?). 我不确定是否有解决这个问题,或者我是否遗漏了一些其他基本原因让我支持这个角落(设计气味?)。 I realize that you generally only want to write tests for the public api, but part of my tests require that I do some setting of private fields. 我意识到你通常只想为公共api编写测试,但我的部分测试要求我对私有字段进行一些设置。

Any suggestions? 有什么建议么?

You could make use of the InternalsVisibleTo attribute to specify that your UnitTest assembly can access internal properties of that class. 您可以使用InternalsVisibleTo属性指定UnitTest程序集可以访问该类的内部属性。

This tightly couples your test and 'real' assembly but this should get you working. 这紧密地结合了你的测试和'真实'组装,但这应该让你工作。

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

相关问题 计算机拥有国的单元测试 - Unit Testing With Computer Owned States 单元测试-我如何测试该课程 - Unit Testing - How do I test this class 单元测试内部属性的反思 - Reflection for unit testing internal properties 我如何对 ExecuteReader() 的内部方法进行单元测试 - How do i unit test ExecuteReader()'s internal method 在 C# Windows Forms 中更改控制状态时,如何忽略触发的简单事件? - How do I ignore simple events firing when changing control states in C# Windows Forms? 如何在FSM中的状态之间创建时间间隔 - How to create time intervals between states in an FSM 如何使用 Moq 模拟 UserRecord 内部 class 以进行 Firebase 单元测试 - How do I mock UserRecord internal class for Firebase unit testing with Moq 如何在单元测试期间设置数据结构的内部状态? - How do I set up the internal state of a data structure during unit testing? 单元测试时,在Navigation属性上调用.CreateSourceQuery()会返回null; 在实际数据库上工作,如何设置测试数据以匹配? - Calling .CreateSourceQuery() on Navigation Property returns null when Unit Testing; works on actual database, how do I setup my test data to match? 如何为我的单元测试创​​建 HttpContext? - How do I create an HttpContext for my unit test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM