Database testing and database Testing
To ensure the correctness of the test at the business layer, we must test the database. However, at present, I did not expect to test the database in the memory. I can only perform integration tests. Next, let's talk about the test on the data layer.
Because in the database, we cannot control that we can only pass the black box test, give the value, and then return the expected results to determine whether the result is successful. However, in the test, we must ensure singularity. This is a metaphor. When we Add a piece of data during the test, the database may generate a piece of dirty data, if you run the test once a day, the consequences are terrible. However, some people say that adding and deleting a production area are not suitable for testing. As a result, you cannot add or delete a test. What should you do if it is modified. So you need to adopt a rollback Mechanism during testing.
Use TransactionScope to roll back and modify data
Let's take an example.
First, we define a TransactionScope
private TransactionScope _scope;
Then initialize in setup
[SetUp] public void SetUp() { this._scope = new TransactionScope(TransactionScopeOption.Required); }
Then we can test the code below.
[Test] public void Create_CreateSuccessful_ReturnsTrue() { var userInfo = CreateUserInfo(); var userDal = new UserDal(); bool result = userDal.Create(userInfo); Assert.IsTrue(result); }
Finally, we release this thing in teardown.
[TearDown] public void TearDown() { this._scope.Dispose(); }
OK, so we have completed the test on the data layer. The test result is as follows:
However, some references to soa only need to be correctly configured.