简体   繁体   中英

Returning an Updated Row in Integration Test C#

In my code I originally have a test method that disable a row by calling a stored procedure. My stored procedure updates the disable column if the value changed from the previous value in the database, otherwise I do a regular update, the regular update exclude the update of the disable column.

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

I have tested this in my SQL Server manager and it works perfectly fine. But when I run it in my test it does not disable the row but instead updates the rows. The first time I was doing it on a pre-existing row in my DB, but later on I inserted my own row before I try to disable it

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

What I did was added a select statement to my insert so that I can get the result set adn see the value of Disable which is false as expected. I then added one select statement in my disable/update stored procedure before doing anything to get a result set to see what disable is, and it is true and I have not done anything yet and as a result it is calling the update section which does not update the disable column. So I do not know what is going on. Below is the output console form my test

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

It must have been called twice: 1) You disabled it 2) Because @Disable parameter == 1 it is same as the existing value so you update the values

Why it was called twice it is the other question - maybe something stupid - like test executed twice or some debugger Watch that executes the command to get the value watched (sounds funny but that is what happened once to me ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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