簡體   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