繁体   English   中英

在Integration Test C#中返回更新的行

[英]Returning an Updated Row in Integration Test C#

在我的代码中,我最初有一个测试方法,该方法通过调用存储过程来禁用行。 如果值从数据库中的前一个值更改,则我的存储过程将更新disable列,否则,我将进行常规更新,该常规更新将禁用禁用列的更新。

IF(@ThingID IS NOT NULL)    
BEGIN      
    SELECT @StoredDisable = Disable FROM Things WHERE ThingID = @ThingID        
    --If the Disable value is different then we are doing   
    IF(@Disable != @StoredDisable)
        BEGIN
            UPDATE Things 
                SET Disable=@Disable
                WHERE ThingID = @ThingID            
            SET @ResultMessage = 'Successfully Deleted'
            SELECT @ResultMessage AS 'Message'                
            RETURN 0
            END     
        END       
    ELSE --If the Disable value is the same then we are just updating the fields  
    BEGIN
            UPDATE Things               
            SET thing1=ISNULL(@thing1, thing1),thing2=ISNULL(@thing1, thing2).....  
                WHERE thingID = @thing1ID                                                                   
            SET @ResultMessage = 'Successfully Updated'
            SELECT @ResultMessage AS 'Message'                                          
            RETURN 0                
    END     
END

我已经在我的SQL Server管理器中对此进行了测试,并且效果很好。 但是,当我在测试中运行它时,它不会禁用该行,而是会更新该行。 第一次在数据库中的现有行上执行此操作,但后来在尝试禁用它之前插入了自己的行

command.Parameters["@ThingID"].Value = DBNull.Value;
command.Parameters["@Comment"].Value = "Inserted from test";

command.ExecuteNonQuery();

 using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
 {
--Insert a new row and got the ordinal and the value of Disable (store in atemp)
--and the thingId (store in result.Id)
Console.WriteLine("the row Disable: {0}",     
    atemp);                                   
 }
  -----------Insert Done----------

 Console.WriteLine("the row id: {0}", result.Id);

 command.Parameters["@ThingID"].Value = result.Id;
 //command.Parameters["@Comment"].Value = "Disable from test";
 command.Parameters["@Disable"].Value = 1;

 connection.Open();
 command.ExecuteNonQuery();

  using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
  {
var tempDisable = false;
var DisableOrdinal = reader.GetOrdinal("Disable");
while (reader.Read())
{
    tempDisable = reader.GetBoolean(DisableOrdinal);
}
Console.WriteLine("the Disable before: {0}", tempDisable);
reader.NextResult();

reader.NextResult();
while (reader.Read())
{
    tempDisable = reader.GetBoolean(DisableOrdinal);
}
Console.WriteLine("the Disable After: {0}", tempDisable);   
 }                      
 ... the rest of code

我所做的就是在我的插入内容中添加了一条select语句,这样我就可以得到结果集并看到Disable的值,该值为预期的错误。 然后,我在禁用/更新存储过程中添加了一条select语句,然后执行任何操作以获取结果集以查看禁用是什么,这是真的,我还没有做任何事情,因此它正在调用update节,不更新禁用列。 所以我不知道发生了什么。 以下是我测试的输出控制台

the row Disable : False
the row id: 1898
the Disable before: True
the Disable After: True  

它必须已被调用两次:1)禁用了它2)因为@Disable参数== 1,它与现有值相同,因此您可以更新这些值

为什么要两次调用它是另一个问题-也许是愚蠢的-两次执行测试,或者某个调试器Watch执行命令以获取监视的值(听起来很有趣,但这就是我一次发生的事情;)

暂无
暂无

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

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