C#/ ASPP.NET MVC单元测试:Nunit - 断言自定义SQL异常

时间:2014-11-28 19:39:34

标签: c# sql .net unit-testing nunit

我正在为现有项目首次编写单元测试。

此时我专注于调用DataAccessLayer

的方法

我有一个方法:

public static bool UpdateRequestCancelledStatus(
  string requestId, 
  string comment, 
  bool startManualWork
)

此方法调用存储过程,该存储过程基于requestId更新记录。

由于我无法依赖任何可用的数据,因此我生成一个空白的requestId:

string requestId = "00000000-0000-0000-0000-000000000000";

此请求不可能存在于表中。 存储过程会抛出自定义异常错误。

我想断言抛出此错误。我正在使用下面的代码,但是,单元测试如果失败,因为它抛出了自定义的sql错误,我期待和sqlexception。如何修改它以期望自定义sql错误。

enter image description here

[Test]
        public void UpdateRequestCancelledStatus()
        {

            string requestId =  Guid.Empty.ToString();
            string comment = "Comment from Unit Test";
            bool startManualWork = false;
            var ex = Assert.Throws<SqlException>(() => nsBusSrvData.RequestMaintenance.OrderDetails.UpdateRequestCancelledStatus(requestId, comment, startManualWork));
            Assert.IsNotNull(ex.Message);
        }

0 个答案:

没有答案
相关问题