简体   繁体   English

C#如何创建用于验证值在代码列表中的单元测试?

[英]C# How to create a unit test for verifying a value is in the code list?

I have an app which receives data, validates it, if no errors are found it saves to the database and if errors are found they get reported but nothing gets to the database. 我有一个可以接收数据,对其进行验证的应用程序,如果未发现错误,则将其保存到数据库中;如果发现了错误,则将报告它们,但不会对数据库进行任何处理。

For example I have the following: 例如,我有以下内容:

CREATE TABLE dimension
(
    dimension_code VARCHAR(10) NOT NULL PRIMARY KEY
);

CREATE TABLE fact_table
(
    fact_id        INT IDENTITY(1,1) NOT NULL,
    dimension_code VARCHAR(10)       NOT NULL,
    CONSTRAINT FK_fact_table 
        FOREIGN KEY (dimension_code) 
        REFERENCES dimension (dimension_code)
);

INSERT INTO dimension VALUES ('A');
INSERT INTO dimension VALUES ('B');

My app allows a user to add a value to fact_table. 我的应用程序允许用户向事实表添加值。 The code (yet to be coded) is supposed to validate that the value the user is trying to add is actually in the dimension table. 该代码(尚待编码)应该用于验证用户尝试添加的值是否确实在维度表中。 If not it will give a nice spelled out error. 如果没有,它将给出一个很好的拼写错误。 If it is then it inserts to the database and a success message is returned. 如果是,则将其插入数据库并返回成功消息。

I am starting with Test Driven Development, so I thought of 2 unit tests: 我从测试驱动开发开始,所以我想到了2个单元测试:

  1. If the value submitted is in the code list, no errors are reported. 如果提交的值在代码列表中,则不会报告任何错误。
  2. If the value submitted is not in the code list, an error is reported. 如果提交的值不在代码列表中,则会报告错误。

I have the classes Dimension and Fact. 我有Dimension和Fact类。 The Dimension class is an Entity Framework class, to check the database values. Dimension类是一个Entity Framework类,用于检查数据库值。 Fact has a validate method which will have many validations including the one about to be written to check against the values in the dimension table. Fact有一种验证方法,该方法将进行许多验证,包括将要编写的验证以对照维表中的值进行的验证。 There is also a list of errors in the app (Error is a class). 应用中还有一个错误列表(Error是一个类)。

I started my first unit test, to create an error if the value is not in the dimension table. 我开始了第一个单元测试,如果值不在尺寸表中,则会产生一个错误。 But as I understand it, a unit test should not read from the database. 但是据我了解,单元测试不应从数据库中读取。 So what should I do, do I create a fake list of values and compare it against that? 那么我该怎么做,是否要创建一个伪造的值列表并将其与之比较? If so, what is the purpose of the test, it will always pass because I am creating the list and the value, so the list would have A,B,C and the value would be D, because I hard coded it into the test. 如果是这样,测试的目的是什么,它将始终通过,因为我正在创建列表和值,所以列表将具有A,B,C,而值将是D,因为我将其硬编码到了测试中。 Or should I read from the database? 还是应该从数据库中读取? Or will my unit test create a Fact instance, assign it an invalid value. 还是我的单元测试将创建一个Fact实例,为其分配一个无效值。 Call validate on it. 呼叫验证就可以了。 And Assert the error list count is now 1 larger than before? 并断言错误列表计数现在比以前大1? But this would read from the database, and my "invalid" value could become valid in the future, the test would fail but my code would still be ok. 但这将从数据库中读取,并且我的“无效”值将来可能变为有效,测试将失败,但是我的代码仍然可以。 Am I not seeing things in the correct perspective? 我没有以正确的视角看待事物吗?

Start with creation of the interface of your app. 从创建应用程序界面开始。 What would you like to have? 你想要点什么? I think it should be some AddFactValue(Fact fact) method? 我认为应该是一些AddFactValue(Fact fact)方法? Then you write a tests which will check what will happen with database in case you run this method with different values. 然后编写一个测试,以检查使用不同值运行此方法时数据库将发生什么情况。 In this process you will find out if you have made something wrong and so on. 在此过程中,您将找出是否做错了什么,等等。

Then, I suppose, you want to add your validator. 然后,我想,您想添加验证器。 Create interface for it and inject it through constructor. 为它创建接口并通过构造函数注入它。 It will allow you to write tests for validator only. 它仅允许您为验证程序编写测试。 So you do the same as with AddFactValue(...) . 因此,您可以执行与AddFactValue(...) And so on... 等等...

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

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