Moq: Creating an integration test with database access


/ Published in: C#
Save to your folder(s)

using (var scope = new TransactionScope()) makes sure that any changes to the database are temporary and only last as long as the test is running.

The ExecuteScalar is used to get a value from the table.

The ExecuteNonQuery is used to insert a record that the test will use.

_target is the class that is accessing the database.


Copy this code and paste it in your HTML
  1. [TestMethod]
  2. public void GetBiAutoProcessConfigByKeys_Test()
  3. {
  4. using (var scope = new TransactionScope())
  5. {
  6. // Setup
  7. var topMx = (int)ExecuteScalar(string.Format("SELECT TOP 1 [mx_company_id] FROM [dbo].[bi_broker_invoice_number]"));
  8. ExecuteNonQuery(string.Format("INSERT INTO [dbo].[bi_auto_process_config] VALUES(39, 15, 1, 0, 1)"));
  9.  
  10. // Action
  11. var result = _target.GetProcessConfigByKeys(15, 39, null);
  12.  
  13. // Assert
  14. Assert.IsNotNull(result);
  15. Assert.IsTrue(result.BusinessUnitId==39);
  16. Assert.IsTrue(result.TransactionTypeId==15);
  17. }
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.